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

Script timeout: exhausted allowed execution time How to fix error?

Asked by
ym5a 52
2 years ago
Edited 2 years ago

So basically I am cloning an NPC 10 times, the script

local val = game.ReplicatedStorage.numberval.Value


for i = 1,10 do
    game.ReplicatedStorage.bacteria:Clone()
    game.ReplicatedStorage.bacteria:Clone().Parent = workspace
    game.ReplicatedStorage.bacteria:Clone().HumanoidRootPart.Position = Vector3.new(math.random(-315.026, -268.827),-148.634, 2.76)
    print("spawned 1wave")
    wait()
end

if val >= 10 then
    for i = 1,10 do
        game.ReplicatedStorage.bacteria:Clone()
        game.ReplicatedStorage.bacteria:Clone().Parent = workspace
        game.ReplicatedStorage.bacteria:Clone().HumanoidRootPart.Position = Vector3.new(math.random(-315.026, -268.827),-148.634, 2.76)
        print("spawned 2wave")
        wait()
    end
elseif val >= 20 then
    for i = 1,10 do
        game.ReplicatedStorage.bacteria:Clone()
        game.ReplicatedStorage.bacteria:Clone().Parent = workspace
        game.ReplicatedStorage.bacteria:Clone().HumanoidRootPart.Position = Vector3.new(math.random(-315.026, -268.827),-148.634, 2.76)
        print("spawned 3wave")
        wait()
    end
elseif val >= 30 then
    for i = 1,10 do
        game.ReplicatedStorage.bacteria:Clone()
        game.ReplicatedStorage.bacteria:Clone().Parent = workspace
        game.ReplicatedStorage.bacteria:Clone().HumanoidRootPart.Position = Vector3.new(math.random(-315.026, -268.827),-148.634, 2.76)
        print("spawned 4wave")
        wait()
    end
elseif val >= 40 then
    for i,v in pairs(game.Workspace:GetChildren()) do
        if v.Name == "bacteria" and v.Humanoid then
            v:remove()
            print("removed waste.....")
        end
    end
end

the first block of code is the important part, this error started when I put the small script below inside the NPC that is being cloned,

--the script is in the model the npc that is being cloned , the npc is stored in replicated storage
wait(1)
while true do --where the stack ends
if script.Parent.Humanoid.Health <= 0 then
    game.ReplicatedStorage.numberval.Value = game.ReplicatedStorage.numberval.Value + 1
wait()
        print("added 1")
    end
end
--this script is where the error occurs, but only occurs once the script above this is enabled, even though the script is enabled in the NPC that is being cloned in replicatedstorage
, so it happens when its cloned

I've tried messing with settings when I enabled the script to clone the NPC it practically crashes roblox I'm trying to make it that once the value = 0 it stops spawning the npcs Before this I made a script practically like this but I ended up removing it. If you have any other ways of trying to perform what I have attempted please do so in the comments

1 answer

Log in to vote
2
Answered by 2 years ago
Edited 2 years ago

While you have added a wait() in the while loop, you placed the wait() in the conditional statement and forgot to consider what happens if the Humanoid's health is above 0. If the Humanoid's health is above 0 it will ignore everything inside the conditional statement and continue looping.

wait(1)
while true do
    if script.Parent.Humanoid.Health <= 0 then
        game.ReplicatedStorage.numberval.Value = game.ReplicatedStorage.numberval.Value + 1
        wait()
        print("added 1")
    end
end

Simply moving the wait() out of the conditional statement, as done below, should fix it:

wait(1)
while true do
    if script.Parent.Humanoid.Health <= 0 then
        game.ReplicatedStorage.numberval.Value = game.ReplicatedStorage.numberval.Value + 1
        print("added 1")
    end
    wait()
end

0
Thanks, but can you fix the first long script, but how do I make the first script keep checking the val number without spawning anymore npcs ym5a 52 — 2y
Ad

Answer this question