Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make an respawning script for NPCs? [closed]

Asked by 5 years ago

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!

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. User#5423 17 — 5y

Closed as Not Constructive by DeceptiveCaster, EpicMetatableMoment, and User#5423

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

2 answers

Log in to vote
1
Answered by 5 years ago

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

Ad
Log in to vote
0
Answered by
BryanFehr 133
5 years ago

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.

0
Thanks! It works! KingCrebz 3 — 5y
0
Anytime! BryanFehr 133 — 5y