Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Does the wait() affect the loop?[Edited]

Asked by 5 years ago
Edited 5 years ago

I made a script with teleportation with all players. I test the game or solo mode it works perfectly but when i test in the local server with 2 players so the event was changed so i was used wait() to all players in the game. Example:

  1. Player1 and Player2 have joined the game.
  2. The game starts to teleport players.(I used Gui to not see the teleportation)
  3. At this time, I think that all players are teleported and tween the gui simultaneously but in the local server, the Player1 is first and the Player2 is last. The music is playing together but the teleportation is like a race.

Can you help me? This is my script.

local Player = game.Players:GetChildren()
for i,v in pairs(Player) do
local Plr = v -- Variable for the v of the for loop
local PlayerGui = Plr:WaitForChild("PlayerGui")
local GameGui = PlayerGui:WaitForChild("Game")
local Teleport = GameGui:WaitForChild("Teleport")
local Character = Plr.Character
local Spawns = Workspace.Minigame1.Spawns:GetChildren()
if Plr then
if Character then
local TargetToTeleport = Character.HumanoidRootPart
if TargetToTeleport then
Teleport:TweenPosition(UDim2.new(0,0,0,0),"In","Quad",1)
wait(1)
local Spawn = Spawns[math.random(1,11)]
Character.HumanoidRootPart.CFrame = Spawn.CFrame
table.remove(Spawns,1)
wait(1)
end
end
end
end

I test another loop It just like I said its do something but on what is the first object in the model in the list was first and last is the last.

local Mosel = game.Workspace.Mosel:GetChildren()
for i, v in pairs(Mosel) do
v:Destroy()
wait(1)
end

1 answer

Log in to vote
0
Answered by 5 years ago

So from what I see your problem is that the players aren't teleported simultaneously.

This is because the for loop will go through everything inside it before repeating itself with the next player. To make it go through all the players without waiting you can use spawn()

Just add this around the code inside your for loop like this:

for i,v in pairs(Player) do
    spawn(function()
        --Do stuff here.
    end)
end

Using spawn(function() end)) will make your loop go through every player without having to wait.

Please upvote if I helped :)

Ad

Answer this question