--[[
	Player management
]]


Ragdoll:SetPlayerDefault(true)

local function characterAdded(player, character)
	if gameStageHandler.currentStage.characterAdded then
		gameStageHandler.currentStage:characterAdded(player, character)
	end

	local humanoid = character:WaitForChild("Humanoid")
	humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, false)

	-- Wait for the avatar to be fully configured
	if not player:HasAppearanceLoaded() then
		player.CharacterAppearanceLoaded:Wait()
	end

	-- Remove accessories that are held in the hand
	for _, instance in pairs(character:GetChildren()) do
		if instance:IsA("Accessory") then
			if
				instance.Handle:FindFirstChild("LeftGripAttachment") or
				instance.Handle:FindFirstChild("RightGripAttachment")
			then
				instance:Destroy()
			end
		end
	end
end

local function playerAdded(player)
	player.CharacterAdded:Connect(function(character)
		characterAdded(player, character)
	end)

	if gameStageHandler.currentStage.playerAdded then
		gameStageHandler.currentStage:playerAdded(player)
	end
end

local function playerRemoving(player)
	if gameStageHandler.currentStage.playerRemoving then
		gameStageHandler.currentStage:playerRemoving(player)
	end
end
