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!
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 the
Humanoid.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.
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?