Npc not respawning what can I do?

Hello!

am making a rpg game.Which means I need npcs to be enemies.
I have made a respawning script it working another npc spawns in but the npc that is dead doesnt go away how can i fix this?

Help need.

1 Like

We don’t have enough information to solve your problem at the moment as it is very vague. It would help to see your code.

1 Like
npc = script.Parent:Clone()

function Died()
	wait(10)
	npc.Parent = script.Parent.Parent
	npc:MakeJoints()
	npc.Human.Health = npc.Human.MaxHealth
	script.Parent:Remove()
end

script.Parent.Human.Died:connect(Died)

this is the respawning code

3 Likes

Are you getting any errors in your output?

3 Likes

I’m pretty sure .Died is a function of a humanoid. Is your humanoid named Human?

2 Likes

Yes but I have fixed it now. I think it’s because I named it human.

Keep your Humanoids named Humanoid. Otherwise, weird functionality takes place or you might not be able to reference them without :FindFirstChildOfClass("Humanoid"). The connections depends on the name of the Humanoid, I’m not sure why but they do.

1 Like

This is false. Humanoid connections are not dependent on the name of a Humanoid. The name has nothing to do with anything. Humanoids not named Humanoid are completely fine and functional.

There’s another underlying issue here that isn’t being resolved, such as the script not being able to run properly. This could easily be a product of oddly written code or changing the structure of how things work out (i.e. having the respawn script externally placed, whether in ServerScriptService or a wrapper model1).

[1]:

Enemy_FOOBAR (wrapper)
    RespawnScript (script)
    FOOBAR (actual NPC)

Example script:

local Container = script.Parent
local StockNPC = Container.FOOBAR:Clone()

local function ConnectRespawn(Humanoid)
    Humanoid.Died:Connect(function ()
        wait(10)
        local NewCharacter = StockNPC:Clone()
        NewCharacter.Parent = Container
        NewCharacter:MakeJoints()
        local NewHumanoid = NewCharacter:FindFirstChildOfClass("Humanoid") do
            NewHumanoid.Health = NewHumanoid.MaxHealth
        end
        Humanoid.Parent:Destroy()
    end
end

Container.ChildAdded:Connect(function (NewChild)
    if NewChild.Name:lower() == "foobar" and maybe_pass_other_checks then
        local Humanoid = NewChild:FindFirstChildOfClass("Humanoid")
        if Humanoid then
            ConnectRespawn(Humanoid)
        end
    end
end)
1 Like