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

How to make an respawning script for NPCs?

Hello, i want to make a script for NPC that when he died, he spawns back to where i originally put the NPC. but i based from scripting. All of them failed. Can you please make me an script that makes an NPC respawn back to the place i orginally putted it. Thanks!

1 comment

0

This is not a request site for scripts. Next time please show us that you have made an attempt by including your code you have used.

2 answers

You can make the NPC reappear in the same spot it originally started if you use cloning from when it is first created

Server Script under the NPC

local npc = script.Parent
local newnpc = npc:Clone()
local startingcframe = npc:WaitForChild("Head").CFrame

npc:WaitForChild("Humanoid").HealthChanged:Connect(function(health)
	if health <= 0 then
		newnpc.Parent = workspace
		newnpc:SetPrimaryPartCFrame(startingcframe)
		npc:Destroy()
	end
end)

First, we clone our npc and retain its original cframe Second, our function will use theHumanoid.HealthChanged` event to detect whether or not we need to reset the NPC

When the health is at or below zero, the reset code will begin

Third, we set the clone's parent to the workspace

We can use :SetPrimaryPartCFrame() to make the npc go back to the starting cframe with the retained value

Finally, we can destroy the previous npc and start over again

I cannot make you a custom script that will respawn your NPC without its variables/credentials, but I can give you a script I used for my ZombieAI NPC's when they respawn. Have these steps:

Make sure you have 2 of the NPC(Duplicate it)

Have 1 in WorkSpace

Have 1 in ReplicatedStorage

Make sure you know where you want the NPC to spawn

Put this script in ServerScriptService

local spawnLocation = game.Workspace:WaitForChild("NPCSpawn") --Where you want the NPC to spawn

while true do
    wait()
    if not game.Workspace.FindFirstChild("NPCNAME") then--Npc name here 
        local CloneNpc = game.ReplicatedStorage:WaitForChild("NPCName"):Clone()
        CloneNpc.Parent = game.Workspace
        CloneNpc.Position = Vector3.new(spawnLocation.Postion)
    end
end

This should work for you.

2 comments

0

Thanks! It works!

0

Anytime!

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