-- ================================================================================
-- PUBLIC FUNCTIONS
-- ================================================================================
-- Puts players into an active players table and runs readyPlayer for each player in that table

function PlayerManager:PreparePlayers()
	-- Get all spawn locations in the folder
	local map = workspace:FindFirstChild("Racetrack")
	if not map then return end
	
	local spawnLocations = map:FindFirstChild("SpawnLocations")
	if not spawnLocations then return end
	
	local availableSpawnPoints = spawnLocations:GetChildren()
	
	-- For every active player, load them into a table, and ready them with everything needed for gameplay
	for _, player in pairs(PlayerManager.ActivePlayers) do
		-- Gets a spawn location and then removes it from the table so the next player gets the next spawn
		local spawnLocation = availableSpawnPoints[1]
		table.remove(availableSpawnPoints, 1)

		PlayerConverter:PlayerToSpeederServer(player, PlayerManager.Selections[player], true)
		readyPlayer(player, spawnLocation)
		CheckpointEvent:FireClient(player, {{"setActive", true}, {"setup"}})
	end
end
	
function PlayerManager:StartPlayer()
	-- For every active player, unanchor their HumanoidRootPart so that they can start racing
	for _, player in pairs(PlayerManager.ActivePlayers) do
		local character = player.Character or player.CharacterAdded:wait()
		local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
		
		if humanoidRootPart then
			humanoidRootPart.Anchored = false
		else
			-- Something is wrong, reload player in lobby
			PlayerConverter:SpeederToPlayerServer(player)
		end
	end
end
