What is wrong with this code?!

Hello! I am freaking out!! I am sitting 2h on this code rn but it wont work!
For your information:
I have a NPC that fights against me. When i kill him i want him to give me a kill and then respawns. But he wont respawn! I tried almost everything. Please help

Script:

killed = false
player = game:GetService("Players").PlayerAdded:Wait()
local Kills = player:WaitForChild("leaderstats"):WaitForChild("Kills")
local Copy = script.Parent:Clone()
local NPC = script.Parent
local spawnpos = 1
local Humanoid
for i,v in pairs(NPC:GetChildren()) do 
	if v:IsA('Humanoid') then 
		Humanoid = v 
	end 
end

while wait() do
	Humanoid.Died:Connect(function()
		if killed == false then
			killed = true
			Kills.Value = Kills.Value + 1
			print("addedkill")
			wait(5)
			print("RESPAWNED")
			local npc1 = script.Parent
			local npc2 = npc1:Clone()
			npc2.Parent=game.Workspace
			npc2:makeJoints()
			npc2:MoveTo(Vector3.new(123.805, 8.173, -321.671))
			script.Parent:Destroy()
			wait(10)
			killed = false
		end
	end)
end

1 Like

Here’s the mistake. The NPC won’t respawn because You are trying to close a dead NPC. Copy the NPC, put it in ServerStorage and when the NPC dies, clone it from the ServerStorage, not from itself.

3 Likes

I’ll try it! Thank you so much

1 Like

It worked thank you very much!! You saved my day :slight_smile:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.