Okay, so I made this loop, and it teleports people and it fire’s all clients that have their name popped up in a folder. But it only does with one player. When multiple players have their name tag inside a folder, it only picks one person. Here is the script:
for i, v in pairs(game.Players:GetPlayers()) do
if game.ReplicatedStorage.PlayerInGame:FindFirstChild(v.Name) then
v.PlayerGui.Core.LoadingFrame.Visible = true
v.PlayerGui.Core.Settings.Visible = false
v.PlayerGui.Core.Menu.Visible = false
for i, c in pairs(game.Workspace.MapFolder:GetChildren()) do
v.Character.Torso.CFrame = CFrame.new(c.Spawn.Tele.Position)
end
game.ReplicatedStorage.Bindables.LoadingStop:FireClient(v)
wait(1)
timer()
else
print("a")
end
end
(No errors were printed)
So if you have any suggestions, please message me! (Please)!
You should probably put the code inside a pcall and a spawn function so it can loop through the code without waiting or being stopped because of an error.
If you’re 100% sure nothing is in the output regarding errors or printing “a”, you are likely yielding the loop with your timer() function. I cant offer you a solution because I don’t know what the timer function does unfortunately.
If you do not wish to share the timer function, please try changing: timer()
to spawn(timer)
local function timer()
for i = currenttimer, 0, -1 do
wait(1)
print(i)
if i == 0 then
for i, v in pairs(game.Players:GetPlayers()) do
ingame = false
if game.ReplicatedStorage.PlayerInGame:FindFirstChild(v.Name) then
v.Character.Humanoid.Health = 0
game.ReplicatedStorage.PlayerInGame:FindFirstChild(v.Name):Destroy()
game.ReplicatedStorage.Bindables.LoseBindable:Invoke(v)
end
end
elseif ingame == false then
break
end
end
end
When you call the timer function, its a loop which counts down from a certain number. When you call a function Lua doesn’t set it to a new thread or coroutine. It just runs inside the current thread meaning the function you called it from has to wait until that function is finished.
To fix this problem, assuming you want this to run for each player, you need to create a new coroutine for this loop to run on. You can do this by using the coroutine functions such as coroutine.create(), but for this case spawn should work well.
Try changing timer() to spawn(timer)
This will create a new thread for the loop to run on, I’d recommend reading up on the function just to get a better understanding of how it works.
IF YOU ONLY WANT THE TIMER FUNCTION TO RUN ONCE… Run the timer function at the end of the for loop (AKA after the end which ends the for loop)
ex:
for i, v in pairs(game.Players:GetPlayers()) do
if game.ReplicatedStorage.PlayerInGame:FindFirstChild(v.Name) then
v.PlayerGui.Core.LoadingFrame.Visible = true
v.PlayerGui.Core.Settings.Visible = false
v.PlayerGui.Core.Menu.Visible = false
for i, c in pairs(game.Workspace.MapFolder:GetChildren()) do
v.Character.Torso.CFrame = CFrame.new(c.Spawn.Tele.Position)
end
game.ReplicatedStorage.Bindables.LoadingStop:FireClient(v)
wait(1)
else
print("a")
end
end
timer() -- Moved timer outside the loop
for i, v in pairs(game.Players:GetPlayers()) do
if game.ReplicatedStorage.PlayerInGame:FindFirstChild(v.Name) then
v.PlayerGui.Core.LoadingFrame.Visible = true
v.PlayerGui.Core.Settings.Visible = false
v.PlayerGui.Core.Menu.Visible = false
for i, c in pairs(game.Workspace.MapFolder:GetChildren()) do
v.Character.Torso.CFrame = CFrame.new(c.Spawn.Tele.Position)
end
game.ReplicatedStorage.Bindables.LoadingStop:FireClient(v)
wait(1)
spawn(timer) -- Just changed this. This will run the timer function for every player.
else
print("a")
end
end
If your intent is to run the timer function only once, then yes, it will teleport the players and then run the timer right afterwards.
There are a few things you could change to make the script a little cleaner, but I dont want to mess with it too much just incase your game is very particular.
There are so many things wrong with the code this thread hurts to read. I’m just going to post some general tips in response to the code here.
This code will teleport the player to every single part in MapFolder one after another. Idk what you’re trying to do.
Also, while pairs does work, ipairs is meant for arrays and is used identically.
game:GetService("Players") and game:GetService("ReplicatedStorage") are the correct way to get these services. The names of these services can be changed via code or in the explorer, and some other services don’t always exist and need to be created via GetService.
This line is likely the root of your problem. It waits one second before continuing to the next player in the list that is created on the first line. This list does not change, so players could leave or join during the loop causing an error or unexpected case. Waiting or yielding in pairs/ipairs loops can cause all sorts of problems, especially if the table changes during iteration.
First of all, changing Gui’s from the server is usually considered bad practice, but I’ll ignore that. What bothers me here is how these objects aren’t being localized when they are used multiple times. I see cases like this all the time. Here’s a less redundant way to write that:
The design here is questionable, but what bothers me is the use of player.Name instead of player.UserId. While player.Name works, it could feasibly be changed during runtime without breaking any rules within the engine.
When you test the game, are you alone or are you testing it in two (with two dummies)? You should not use pairs but ipairs, because the function returns an array and ipairs is suitable to be used in arrays to yes, let’s just say that you should use the ipairs function. Then please add this to the second line: