It gets stuck in “Waiting For Players” even if there are more than 2 players
local players = game:GetService(“Players”)
local status = replicatedstorage:WaitForChild(“status”)
while true do
wait(0)
local plrs = #players:GetPlayers()
while plrs < 2 do
status.Value = “Waiting For Players…”
repeat wait() until plrs >= 2
end
end
I mean, will repeat wait() until there are more of two players, but what will happend when there are two players? wait() will stop, nothing more! There’s another function when there are more of two players?
Essentially, your while loop is waiting for the plrs variable to change. However, this variable won’t change. While you’ve put the one :GetPlayers() on the outside, you’ll additionally need to put it in the inside of the loop as well, so that the list updates.
Your new code would look like this
local players = game:GetService(“Players”)
local status = replicatedstorage:WaitForChild(“status”)
while true do
wait(0)
local plrs = #players:GetPlayers()
while plrs < 2 do
plrs = #players:GetPlayers() -- NEW LINE HERE
status.Value = “Waiting For Players…”
repeat wait() until plrs >= 2
end
end
And if you really want to optimise this set of code alot better, you can try this:
local players= game:GetService(“Players”)
local status = replicatedstorage:WaitForChild(“status”)
while true do
wait()
local plrs = #players:GetPlayers()
if plrs < 2 then
status.Value = “Waiting For Players…”
repeat
wait()
plrs = #players:GetPlayers() -- NEW LINE HERE
until plrs >= 2
status.Value = “All players are here” -- or whatever your usual message is
end
end
That way, you don’t have to change the value of Status each cycle, and you also save lines of code.
local players = game:GetService(“Players”)
local status = replicatedstorage:WaitForChild(“status”)
while true do
wait(0)
local plrs = players:GetPlayers() --I think you would use just "players"
while #plrs < 2 do --I think you would also put "#plrs"
status.Value = “Waiting For Players…”
repeat wait() until #plrs >= 2
end
end