While true do bug my LoadingScreen (Loading Text)

Hello, today I was developing a 1v1 system, that is why I created a lobby to grab the players and teleport them to a place with a maximum of 2 players. I was adding some decorations and I came across something that I don’t know how to do.

ERROR :
The error that I don’t know how to fix is that when there is 1 player and someone enters, the first player who entered gets (1/2) and stays bugged and doesn’t go to (2/2) if someone joins
IMAGE

while task.wait() do
	local players = 0
	for i, v in ipairs(game.Players:GetPlayers()) do
		players += 1
	end
	if players >= 2 then
		print("There are enough players! (2/2)")
		--//code
		while true do
			script.Parent.TextLabel.Text = "Looking for Players!."
			script.Parent.Co.Text = "(2/2)"
			wait(0.5)
			script.Parent.TextLabel.Text = "Looking for Players!.."
			script.Parent.Co.Text = "(2/2)"
			wait(0.5)
			script.Parent.TextLabel.Text = "Looking for Players!..."
			script.Parent.Co.Text = "(2/2)"
			wait(0.5)
			script.Parent.TextLabel.Text = "Looking for Players!..."
			script.Parent.Co.Text = "(2/2)"
			wait(0.5)
			script.Parent.TextLabel.Text = "Looking for Players!..."
			script.Parent.Co.Text = "(2/2)"
			wait(0.5)
			script.Parent.TextLabel.Text = "Looking for Players!..."
			script.Parent.Co.Text = "(2/2)"
			wait(0.5)
			script.Parent.TextLabel.Text = "Looking for Players!..."
			script.Parent.Co.Text = "(2/2)"
			wait(0.5)
			print("Teletransporting to 1v1 Place...")
	
		end

		
	else 
		warn("Not enough players yet! (1/2)")
		while true do
			script.Parent.TextLabel.Text = "Looking for Players!."
			script.Parent.Co.Text = "(1/2)"
			wait(0.5)
			script.Parent.TextLabel.Text = "Looking for Players!.."
			script.Parent.Co.Text = "(1/2)"
			wait(0.5)
			script.Parent.TextLabel.Text = "Looking for Players!..."
			script.Parent.Co.Text = "(1/2)"
			wait(0.5)	
			
		end

	end
end

The first player keeps getting (1/2), if someone knows the error please let me know (i use translator sorry) :slightly_smiling_face:

Just put wait() inside the loop (at the beginning or end).

while task.wait() do 
print("Work")
wait(1)
end

while true do
wait(1)
print("Work")
end

while true do
print("Work")
wait(1)
end

EDIT: Do not put a loop inside the loop, it is a bad practice.

Replace the pairs loop with this:

local players = 0

for Index, Player in next, game.Players:GetPlayers() do
    players += 1
    task.wait(0.1)
end

In addition, DONT add a while loop for the whole entire script; It will lag, cause errors, and loop through all the players every time the loop iterates, aswell as the players variable will get to be over 2. Possibly try RunService.Heartbeat as an option!

@piranca @CommanderRanking I already tried the 2 ways and they didn’t work for me, what I want to do is that when someone joins that lobby for the first time it exits (1/2) however by the while true do it bugs and when someone joins it is still in 1 /two. Instead the player who joined after the first if he gets 1/2

local players = 0

game.Players.PlayerAdded:Connect(function(player)
players += 1
end)

while true do
if players >= 2 then
print(“There are enough players! (2/2)”)
–//code
script.Parent.TextLabel.Text = “Looking for Players!.”
script.Parent.Co.Text = “(2/2)”
wait(0.5)
script.Parent.TextLabel.Text = “Looking for Players!..”
script.Parent.Co.Text = “(2/2)”
wait(0.5)
script.Parent.TextLabel.Text = “Looking for Players!..”
script.Parent.Co.Text = “(2/2)”
wait(0.5)
script.Parent.TextLabel.Text = “Looking for Players!..”
script.Parent.Co.Text = “(2/2)”
wait(0.5)
script.Parent.TextLabel.Text = “Looking for Players!..”
script.Parent.Co.Text = “(2/2)”
wait(0.5)
script.Parent.TextLabel.Text = “Looking for Players!..”
script.Parent.Co.Text = “(2/2)”
wait(0.5)
script.Parent.TextLabel.Text = “Looking for Players!..”
script.Parent.Co.Text = “(2/2)”
wait(0.5)
print(“Teletransporting to 1v1 Place…”)
else
warn(“Not enough players yet! (1/2)”)
script.Parent.TextLabel.Text = “Looking for Players!.”
script.Parent.Co.Text = “(1/2)”
wait(0.5)
script.Parent.TextLabel.Text = “Looking for Players!..”
script.Parent.Co.Text = “(1/2)”
wait(0.5)
script.Parent.TextLabel.Text = “Looking for Players!..”
script.Parent.Co.Text = “(1/2)”
wait(0.5)
end
wait(.1)
end

EDIT: I recommend using “while true do” or “RunService Heartbeat”

it is better to use a variable for a while true do loop. that way, you can break out of the loop more conveniently.

local Players = game:GetService("Players")
local running = true
local dots = {".", "..", "..."}

coroutine.wrap(function()
    Players.PlayerAdded:Connect(function()
        if #Players:GetChildren() >= 2 then
            running = false
            script.Parent.TextLabel.Text = "Starting Game!"
        end
    end)
end)()

while running do
    for i, v in ipairs(dots) do
        script.Parent.TextLabel.Text = "Looking for Players!" .. v
    end
end

I get some errors

``
local players = 0

	while task.wait() do
		for i, v in ipairs(game.Players:GetPlayers()) do
			players += 1
		end
		if players >= 2 then
			print("There are enough players! (2/2)")

			script.Parent.TextLabel.Text = "Looking for Players!."
			script.Parent.Co.Text = "(2/2)"
		    wait(0.5)
			script.Parent.TextLabel.Text = "Looking for Players!.."
			script.Parent.Co.Text = "(2/2)"
		    wait(0.5)
			script.Parent.TextLabel.Text = "Looking for Players!..."
			script.Parent.Co.Text = "(2/2)"
		    wait(0.5)
			script.Parent.TextLabel.Text = "Looking for Players!."
			script.Parent.Co.Text = "(2/2)" do
			wait(0.5)
			print("Teletransporting to 1v1 Place…")
		else
			warn("Not enough players yet! (1/2)")
			script.Parent.TextLabel.Text = "Looking for Players!."
			script.Parent.Co.Text = "(1/2)"
			wait(0.5)
			script.Parent.TextLabel.Text = "Looking for Players!.."
			script.Parent.Co.Text = "(1/2)"
			wait(0.5)
			script.Parent.TextLabel.Text = "Looking for Players!..."
			script.Parent.Co.Text = "(1/2)"
			wait(0.5)
		
		wait(0.1)
	end
end
end

local players = 0

game.Players.PlayerAdded:Connect(function(player)
players += 1
end)

while true do
if players >= 2 then
print(“There are enough players! (2/2)”)
–//code
script.Parent.TextLabel.Text = “Looking for Players!.”
script.Parent.Co.Text = “(2/2)”
wait(0.5)
script.Parent.TextLabel.Text = “Looking for Players!..”
script.Parent.Co.Text = “(2/2)”
wait(0.5)
script.Parent.TextLabel.Text = “Looking for Players!..”
script.Parent.Co.Text = “(2/2)”
wait(0.5)
script.Parent.TextLabel.Text = “Looking for Players!..”
script.Parent.Co.Text = “(2/2)”
wait(0.5)
script.Parent.TextLabel.Text = “Looking for Players!..”
script.Parent.Co.Text = “(2/2)”
wait(0.5)
script.Parent.TextLabel.Text = “Looking for Players!..”
script.Parent.Co.Text = “(2/2)”
wait(0.5)
script.Parent.TextLabel.Text = “Looking for Players!..”
script.Parent.Co.Text = “(2/2)”
wait(0.5)
print(“Teletransporting to 1v1 Place…”)
else
warn(“Not enough players yet! (1/2)”)
script.Parent.TextLabel.Text = “Looking for Players!.”
script.Parent.Co.Text = “(1/2)”
wait(0.5)
script.Parent.TextLabel.Text = “Looking for Players!..”
script.Parent.Co.Text = “(1/2)”
wait(0.5)
script.Parent.TextLabel.Text = “Looking for Players!..”
script.Parent.Co.Text = “(1/2)”
wait(0.5)
end
wait(.1)
end

Try this code.

No work :frowning: I already tried it but now it only comes out 1/2

A loop isn’t even necessary for this, you should just use Players:PlayerAdded() and Players:PlayerRemoving(). When a player first joins, find the initial amount of players by just doing len(Players:GetPlayers()) (I think that should work, I haven’t tested it, if that doesn’t work, I believe adding .len() to the end of the list will work.

EDIT: Then you only need to update the UI when a player is added or removes.

You need to add in the code player = player + 1 (not player += 1).

Those two statements do the exact same thing, there is no difference in their function. player += 1 is more concise, so in most cases it is better practice to use it.

Actually, they’re the same, they’re both syntactical sugar. Usually though, as you stated, += is the way to go to optimize the code.

the proper way to get the amount of [instances or whatever] in a table is by using the # operator

local idk = {"i", "d", "k"}

print(#idk) --prints 3

table.remove(idk, 1)

print(#idk) --prints 2