Why does my NPC Respawner only work once?

I’m trying to write an NPC Respawner that automatically respawns an NPC once it dies. However, for some reason, it only works once. I honestly have no idea what’s causing this. Neither the Script Editor nor Output is showing errors. Any help is appreciated. If you need more info/screenshots, I’d be happy to try and get them.

Expected Behavior: Once the NPC Practice Dummy dies, it is deleted and replaced immediately. The replacement takes the place of the original.

Current Behavior: If the NPC dies, it’ll be deleted and replaced. However, if the replacement dies, nothing happens.


Script:


local Dummy = workspace["Practice Dummies"]["Practice Dummy One"]




local Variable




local function Respawn()
	print("Died.")
	Dummy:Remove()
	local Respawned = script["Practice Dummy One"]:Clone()
	Respawned.Torso.Position = Vector3.new(-33.5, 4.5, -49.5)
	Respawned.Parent = workspace["Practice Dummies"]
	Dummy = Respawned
	Variable:Disconnect()
	print(Dummy.Humanoid.Health)
	print(Dummy.Parent)
end




Variable = Dummy.Humanoid.Died:Connect(Respawn)


Hierarchy

image


image

1 Like

I think it’s because Variable is set to an event of the Dummy’s Humanoid that already exists, but if it dies it doesn’t make a new connection for the new Dummy’s Humanoid.

Yeah, I thought that was what was happening, that’s why I added a Disconnect. But, it seems like that didn’t do anything. I haven’t really used Disconnect before, so I don’t know if I’m using it wrong.

adding Variable = Dummy.Humanoid.Died:Connect(Respawn) at the end of Respawn() should work

1 Like

The variable is set to a connection, not the event.


Remove() is deprecated, Destroy() is current. Also you disconnected the connection, ending the loop, meaning nothing is set to Variable.

1 Like

It baffles me how I work on a simple script for over an hour, and someone form DevForum, solves it in a minute. Thanks for the help!

1 Like