How can you change :GetPlayers into a table?

Yes, I’ve made another post today because I can’t find the solution for this, sorry for that. I’ve been trying to make a for loop that gets all the players, but I can’t figure out how to change the :GetPlayers into a table, and I can’t find an answer on Youtube.

Error: ServerScriptService.Script:16: invalid argument #1 to ‘pairs’ (table expected, got number) - Server - Script:16

local IntermisssionTime = 5
local RoundTime = 90
local Intermission = 0
local Round = 0
local Spawns = {
	Spawn1 = game.Workspace.Spawns.Spawn1,
	Spawn2 = game.Workspace.Spawns.Spawn2,
	Spawn3 = game.Workspace.Spawns.Spawn3,
	Spawn4 = game.Workspace.Spawns.Spawn4,
	Spawn5 = game.Workspace.Spawns.Spawn5,
	Spawn6 = game.Workspace.Spawns.Spawn6
}

function RoundTimer()
	Round = RoundTime
	for i,v in pairs(#game.Players:GetPlayers()) do
		local randomSpawn = math.random(1, Spawns[0])
		game.Players:FindFirstChild("HumanoidRootPart").CFrame = randomSpawn.CFrame
	end
	while Round > 1 do
		wait(1)
		Round = Round - 1
		print(Round)
	end
	if Round < 1 then
		print("Round over")
	end
end

game.Players.PlayerAdded:Connect(function()
	local playerCount = #game.Players:GetPlayers()
	print(playerCount)
	if playerCount == 1 and Round < 1 then
		Intermission = IntermisssionTime
		while Intermission > 1 do
			wait(1)
			Intermission = Intermission - 1
			print(Intermission)
		end
		if Intermission == 1 then
			print("Intermission over")
			RoundTimer()
		end
	end
end)

Remove the # before game.Players:GetPlayers() when you are passing it as an argument to pairs().

game.Players:GetPlayers() already returns a table of the players; the hashtag should only be used for getting the number of items in the table.

3 Likes

In the code, you don’t put # before a table, that will give you the number of children IN that table, and not return the table itself. (I repeated tables a lot)

1 Like

To sum up in visual what everyone is saying

this

for _,v in ipairs(game.Players:GetPlayers()) do
    -- Do things
end
1 Like

Ah, I did not know that that’s what # did

3 Likes