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

How do I rest the while loop?

I have a script that spawns a random NPC from a folder onto a random block that is in another folder. I set a max number of 5 so no more than 5 NPCs will spawn. But how do I set it so they respawn once one dies. Or How do I rest the code once all spawned NPCs have died.

local NPCS = game.ReplicatedStorage.Enemies:GetChildren()
local number = 0

if number >= 0 then
        canspawn = true
        print("can spawn")
    end 
while  canspawn do
    local ChosenNPC = NPCS[math.random(1, #NPCS)]
    local ChosenSpawn = math.random(1, 6)
    print(ChosenNPC)
    print(ChosenSpawn)
    wait(5)
    if ChosenNPC then
    clone = ChosenNPC:Clone()
        number = number + 1     
        print(number)

        clone.HumanoidRootPart.Position = workspace.EnemySpawns:FindFirstChild("Spawn"..ChosenSpawn).Position
        clone.Parent = workspace.EnemySpawns.Spawned
        clone:MakeJoints()
        if number >= 5 then
            canspawn = false
            print("Stoping spawning")
        end 



    end

end

1 answer

✓ Accepted answer

Stop the loop with break.

while canspawn do
	
	if number >= 5 then
		
		canspawn = false
		break
	end
end

If you ever want to activate the loop again, put the loop into a function.

Example:

function loop()
	while true do
		
	end
end

loop() -- activates the loop

1 comment

0

Thanks for the tip

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