#players script not working

I have this loop for the game, and it checks how many players there are…

while true do
local players = game.Players:GetPlayers()
if #players >= 1 then
print(‘Enough Players’)
wait()
end
end

This is the script, it doesn’t seem to print ‘Enough Players’

This worked for me.

while true do
	local players = game.Players:GetPlayers()
	if #players >= 1 then
		print("Enough Player")
	end
	wait()
end
3 Likes

I improved readability a bit here for you:

while true do
    wait()
    if #game.Players:GetPlayers() >= 1 then
        print("Enough Players")
    end
end

Otherwise, I see no issues with your script. Make sure it’s enabled and that it’s in ServerScriptService or Workspace.

EDIT: @Halalaluyafail3 has pointed out your error.

1 Like
while true do
    local players = game.Players:GetPlayers()
    if #players >= 1 then
        print(‘Enough Players’)
        wait()
    end
end

This creates an infinite loop if there isn’t 1 player immediately, as it only yields with 1+ players.

Perhaps you meant to yield every time?

while true do
    local players = game.Players:GetPlayers()
    if #players >= 1 then
        print(‘Enough Players’)
    end
    wait()
end
2 Likes

Please do not use wait as a condition for a while loop.

2 Likes

Huh. I’ve always used it and never had any issues. I’ll give the article a read and see what it says. In the mean time, I’ve edited my post.

Yeah, the problem isn’t that it doesn’t work, it’s that it has various bad practice and performance implications. It’s bad code. Non-terminating loops should always use a truthy value for a condition and any other while loop should use said condition properly.

1 Like