-- handles all the events incoming from the client


local keyModule = require(script.Parent:WaitForChild("Game Logic").KeyModule)

local function cleanupCharacter(char)
    
    -- drop the tools in the spot
    if char:FindFirstChild("HumanoidRootPart") then
        keyModule.DropTools(player,game.Workspace.Map,char.HumanoidRootPart.Position)
        print("Tools Dropped")
    end

    -- remove the tag from their player
    if player:FindFirstChild("Contestant") then
        player.Contestant:Destroy()
        print ("contestant tag dropped")
    elseif player:FindFirstChild("Piggy") then
        player.Piggy:Destroy()
        print ("piggy tag dropped")
    end
end

local function onCharacterAdded(char)
    -- drop all the tools that the player is holding
    char.Humanoid.Died:Connect(cleanupCharacter)
    
end

local function onPlayerAdded(player)
        -- add a tag to players when joining the game; this tag is removed when players click on 'play'
        -- prevents AFK players from joining the game
        local inMenu = Instance.new("BoolValue")
        inMenu.Name = "InMenu"
        inMenu.Parent = player    

        -- this function fires when the character spawns into the game (i.e. after the die / reset their character
        player.CharacterAdded:Connect(onCharacterAdded)
end
