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

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

Asked by 5 years ago
Edited 5 years ago

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
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")* EpicMetatableMoment 1444 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago

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
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Fixed, Needed To Rename The Zombies To A Number And Needed To Add A Wait Thanks

0
I'll accept sinister's answer for you. But please get into the habit of accepting answers that solve your problem User#24403 69 — 5y

Answer this question