I have a respawn script I want to cap it so the npc respawns only 5 time when killed

I have a respawn script I want to cap it so the npc respawns only 5 time when killed can someone please look at it and help me

local RespawnTime = 5

while true do
	character = script.Parent
	clone = character:Clone()
	character.Humanoid.Died:wait()
	wait(RespawnTime)
	clone.Parent = script.Parent.Parent
	script.Parent = clone
	character:Destroy()
end
1 Like

if respawntime > 0 then
respawntime = respawntime - 1

1 Like

ok but how is this going to cap the respawn at 5 times

1 Like

if the respawntime is larger than zero, it will subtract 1 from respawntime
didnt u already made a function that has the value of 5? dont have to cap it

1 Like

ok I will try it :thinking: :thinking:

btw i havent tested this in studio yet lol

local RespawnTime = 5

while true do
	character = script.Parent
	clone = character:Clone()
	character.Humanoid.Died:wait()
	wait(RespawnTime)
if RespawnTime > 0 then
RespawnTime = RespawnTime - 1	
clone.Parent = script.Parent.Parent
	script.Parent = clone
	character:Destroy()
end
end

ok :thinking: :thinking: :smile: :smile:

Try putting your while true code inside a for loop.

local RespawnTime = 5

for i = 1, 5 do
	character = script.Parent
	clone = character:Clone()
	character.Humanoid.Died:wait()
	wait(RespawnTime)
	clone.Parent = script.Parent.Parent
	script.Parent = clone
	character:Destroy()
end

Or you can use a variable that’s a number and increases each time the the character dies, if it’s 5 or above then you’ll break the loop.