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)
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
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
``
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
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.
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.