Title freezes just like a while true do
loop
Server Script
while wait(5) do if game.Lighting.ClockTime >= 18 or game.Lighting.ClockTime <= 7 then local zombies = game.ServerStorage.Zombies:GetChildren() while math.random(1,3) do for i = 1 , #zombies do if i == math.random(1,#zombies) then local c = zombies[i]:Clone() c.HumanoidRootPart.CFrame = CFrame.new(math.random(-50,50), 5 ,math.random(-50,50)) end end end end end
output:
13:13:16.468 - Game script timeout 13:13:16.470 - Stack Begin 13:13:16.470 - Script 'ServerScriptService.ZombieSpawnScript', Line 5 13:13:16.471 - Stack End
Just freezes and doesn't spawn the zombies, how do i fix it?
You're yielding the entire game when you make a while (condition) do end
loop (assuming (condition) is a truthy statement)
In this case you're doing while math.random(1,3) do ... end
with no sort of yield. math.random(...)
returns a number and numbers have truthiness. Example,
if 1 then print"truthy" end
This will output "truthy"
when ran since 1
is a number and numbers have truthiness.
Yield your second while do
loop to prevent it from hanging the game.
--// *(assume condition's are true)* --// this will hang your game while (condition) do --// end --// this will not hang your game while (condition) do --// wait() end
while wait(5) do if game.Lighting.ClockTime >= 18 or game.Lighting.ClockTime <= 7 then local zombies = game.ServerStorage.Zombies:GetChildren() while math.random(1,3) do for i = 1 , #zombies do if i == math.random(1,#zombies) then local c = zombies[i]:Clone() c.HumanoidRootPart.CFrame = CFrame.new(math.random(-50,50), 5 ,math.random(-50,50)) end end wait() --// yields for 0.03 seconds to prevent game from hanging end end end
Fixed, Needed To Rename The Zombies To A Number And Needed To Add A Wait Thanks