rin5e
(blackzeus)
July 12, 2019, 6:41pm
#1
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.
EDIT (08/09/2021): Yes, I’ve actually started seeing while task.wait(). Just so it’s clear, the document does cover yielding functions being used as conditions as well. task.wait() is not a better replacement and also falls under discouraged practice as per the below.
Introduction
Earlier today on my usual camp-out by the Scripting Support category, I stumbled across a piece of code that set off my nitpick ringer and reminded me of a document I stumbled across long ago. Today, after being prom…
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