Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Just freezes and doesn't spawn the zombies, how do i fix it?

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

1 comment

0

You have no yields in your second while loop and so it hangs the main thread. Put a yield of some sort such as wait() in your second while loop. (your second while loop is the one with "while math.random(1, 3) do")

2 answers

✓ Accepted answer

###Accept this answer if it helped!!

##Problem

Just freezes and doesn't spawn the zombies, how do i fix it?

##Whats wrong 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.

##Solution 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

###Code to copy if you're lazy or confused

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

1 comment

0

I'll accept sinister's answer for you. But please get into the habit of accepting answers that solve your problem

This discussion is preserved as it was. New answers, comments, and votes are closed.