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?
01 | for _, player in pairs (game.Players:GetPlayers()) do |
02 | if player.Character then |
03 | if player.CurrentLocation.Value = = "Bunker1" and bunkerOne.DoorClosed.Value = = false then --If bunker one door is open |
04 | repeat |
05 | wait( 1 ) |
06 | game.Workspace [ player.Name ] .Humanoid.Health = game.Workspace [ player.Name ] .Humanoid.Health - 10 |
07 | until game.Workspace [ player.Name ] .Humanoid.Health = = 0 or bunkerOne.DoorClosed.Value = = true |
08 | else |
09 | if magnitude < = maxDistanceBunker 1 and player.CurrentLocation.Value = = "Bunker1" then --If another player is in vault one |
10 | repeat |
11 | wait( 1 ) |
12 | game.Workspace [ player.Name ] .Humanoid.Health = game.Workspace [ player.Name ] .Humanoid.Health - 10 |
13 | until game.Workspace [ player.Name ] .Humanoid.Health = = 0 |
14 | end |
15 | end |
16 | end |
17 | end |
18 | end |
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.
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