Attempting to loop Players:GetPlayers(), but returns 0

I’m trying to create a round system in which the round does not start until the number of players is above a certain number. However, I can’t seem to either get the number of players correctly, or loop the check until the number is above a threshold.

local Players = game:GetService("Players")
local players = Players:GetPlayers()

local function RoundSetup()
	local playersCount = 0
	for _, player in pairs(players) do
		if player.Character then
			playersCount += 1 -- this returns 0, at least when I'm testing in Studio
		end
	end
	if playersCount >= 3 then
		Round()
	else -- trying to keep the script checking whether the amount of players is more than 3
		repeat
			local playersArray = players
			local playersCount = #playersArray
			print("Number of players: " .. playersCount) -- 0
			task.wait(5)
			RoundSetup()
		until playersCount >= 3
		RoundSetup()
	end
end
However, if that shouldn't be the issue, I have a part where I move the character appearances to a folder in the workspace.
Players.PlayerAdded:Connect(function(player)
	player.CharacterAppearanceLoaded:Connect(function(character)
		RunService.Stepped:Wait()
		character.Archivable = true
		character.Parent = workspace.Appearances
	end)
end)

this might be some inefficient code as I’m starting out but it seems like i got stuck here and i can’t seem to find anything of help…

I believe

local players = Players:GetPlayers()

should go in the function.

The script only searches for the players once the script is created and isn’t called upon again.

1 Like

You don’t need to do this just simply use #

if #Players:GetPlayers() >= 3 then
    ...
end

Also what you did at first was stored the Players:GetPlayers() on a variable which in this case will stay empty if run on start.

3 Likes

How stupid of me lol. Calling Players:GetPlayers() during the for loop worked.

The loop code might be a bargain but at least I got it working.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.