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

How To Combine Several Repeat Loops?

Asked by 6 years ago

So I am working on a game with nukes, and below are some of the ways I detect the location of each player and determine if they are able to survive. All of the code works however, I ran into an issue when there are multiple players, the repeats will repeat until that player is dead and then it will effect the others. How can I make it so that everyone takes damage at the same time?

for _, player in pairs(game.Players:GetPlayers()) do
    if player.Character then
        if player.CurrentLocation.Value == "Bunker1" and bunkerOne.DoorClosed.Value == false then --If bunker one door is open
            repeat
                wait(1)
            game.Workspace[player.Name].Humanoid.Health = game.Workspace[player.Name].Humanoid.Health - 10
            until game.Workspace[player.Name].Humanoid.Health == 0 or bunkerOne.DoorClosed.Value == true
        else
            if magnitude <= maxDistanceBunker1 and player.CurrentLocation.Value == "Bunker1" then --If another player is in vault one
                repeat
                    wait(1)
                    game.Workspace[player.Name].Humanoid.Health = game.Workspace[player.Name].Humanoid.Health - 10
                until game.Workspace[player.Name].Humanoid.Health == 0
            end
        end     
    end
end
end

2 answers

Log in to vote
2
Answered by
Dog2puppy 168
6 years ago

You could wrap what’s inside the first for loop in a spawn() function. This makes a new thread so the main thread isn’t blocked.

Ad
Log in to vote
1
Answered by 6 years ago

The script is halting because of the waits, to get around this please look up coroutines, they can run in another priority without halting the rest of the script.

Here's a link to get you started

http://wiki.roblox.com/index.php/Beginners_Guide_to_Coroutines

Answer this question