Trying to make a safezone for my game, using a region for the safezone and a region for the world itself. Whichever function I put first is the only one that runs. Can someone help me with this or give me a better method of doing it?
while JB do wait(0.5) local partsInJB = workspace:FindPartsInRegion3(jbRegion) for i, part in pairs(partsInJB) do if part.Parent:FindFirstChild("Humanoid") ~= nil then player = game.Players:GetPlayerFromCharacter(part.Parent) char = player.Character currentZone = "Jame's Bay" print(player.Name.." found in JB") newGui(player,currentZone) end end end while world do wait(0.5) local partsInWorld = workspace:FindPartsInRegion3(worldReg) for i, partw in pairs(partsInWorld) do if partw.Parent:FindFirstChild("Humanoid") ~= nil then player = game.Players:GetPlayerFromCharacter(partw.Parent) chawr = player.Character currentZone = "Leaving SafeZone..." print(player.Name.." is leaving the SZ") newGui(player,currentZone) end end end
Because any sort of loop is going to be ran before any code before it, with a while ... do loop, it will repeat that loop until it "breaks" or a value is set to false.
To keep it simple, you can use a coroutine function to run new threads in the same script.
This way, you can run 2 while loops without them disrupting eachother.
local JB = true local world = true local JBLoop = coroutine.wrap(function() while JB do wait(0.5) local partsInJB = workspace:FindPartsInRegion3(jbRegion) for i, part in pairs(partsInJB) do if part.Parent:FindFirstChild("Humanoid") ~= nil then player = game.Players:GetPlayerFromCharacter(part.Parent) char = player.Character currentZone = "Jame's Bay" print(player.Name.." found in JB") newGui(player,currentZone) end end end end) local worldLoop = coroutine.wrap(function() while world do wait(0.5) local partsInWorld = workspace:FindPartsInRegion3(worldReg) for i, partw in pairs(partsInWorld) do if partw.Parent:FindFirstChild("Humanoid") ~= nil then player = game.Players:GetPlayerFromCharacter(partw.Parent) chawr = player.Character currentZone = "Leaving SafeZone..." print(player.Name.." is leaving the SZ") newGui(player,currentZone) end end end end) JBLoop() worldLoop() --// Everything will run without it being disturbed by the loops, coroutines are very useful! print("Works!")
Hope this helped! Feel free to select this as an answer if this worked for you!
Try using two separate scripts, as it looks like your first loop is constantly going and therefore the script cannot proceed? Script 1
while JB do wait(0.5)
local partsInJB = workspace:FindPartsInRegion3(jbRegion) for i, part in pairs(partsInJB) do if part.Parent:FindFirstChild("Humanoid") ~= nil then player = game.Players:GetPlayerFromCharacter(part.Parent) char = player.Character currentZone = "Jame's Bay" print(player.Name.." found in JB") newGui(player,currentZone) end end `end`
(script 2 begin)
while world do
wait(0.5) local partsInWorld = workspace:FindPartsInRegion3(worldReg) for i, partw in pairs(partsInWorld) do if partw.Parent:FindFirstChild("Humanoid") ~= nil then player = game.Players:GetPlayerFromCharacter(partw.Parent) chawr = player.Character currentZone = "Leaving SafeZone..." print(player.Name.." is leaving the SZ") newGui(player,currentZone) end end
end