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