heya scripters!
i got a big problem, because i cant manage to make my script work properly!
i want an npc to respawn, but it doesnt if it dies by the world void!
i have no idea why the .Died Event isnt working. /:
WHAT HAVE I TRIED?
-using bindable events
-putting a clone of the npc in replicated and serverstorage and cloning it into workspace
-using childremoved event to detect the death and initiate the respawning
-asking my best friend for help, but he said that the only way to do it is detecting how far the npc falls /:
my nerves are at the end and i really need help by some nice scripters that would like to help me
thanks for replying!
local Time = script.RESPAWN_TIME.Value
local new = script.Parent:Clone()
script.Parent.Humanoid.Died:connect(function()
task.wait(Time)
new:MakeJoints()
new.Parent = workspace
task.wait(0.2)
script.Parent:Destroy()
end)
Only the Destroyed event is called when an object is destroyed, and all connections to events in the destroyed instance are forgotten. So the Humanoid.Died event is forgotten. Change your code like this:
local Time = script.RESPAWN_TIME.Value
local NPC = game.ReplicatedStorage.NPC -- This will be cloned! Make sure your template NPC is here
local old = script.Parent
local function Respawn()
local new = NPC:Clone()
task.delay(time, function()
new.Parent = workspace
if old then
old:Destroy()
end
old = new
end)
end
script.Parent.Humanoid.Died:Connect(Respawn)
script.Parent.Destroyed:Connect(Respawn)
It could be a lot better but I don’t know how your game works.
What I’ve always done is stored a copy of the NPC in server storage and then written code that handles the respawning of all entities in the game. Usually, this means listening for the .Died event and/or the Destroyed event. This way when the event occurs I can just clone the copy I stored and place it back in the game.
I’m on phone so sorry if it’s wrong
Make a server script in serverscriptservice
Define the npc (like local npc = yourPath)
Make the server check if the npc is dead or its health is set to 0
Have the server respawn it for you. LocalScripts only do stuff locally, if you put a localscript, respawning wouldn’t necessarily work
In order to create an NPC respawn system, first we need to start where the NPCs are spawned.
Whenever an NPC is spawned, we need to attach a function, Respawn, to whenever the NPC dies or the NPC falls out of the world.
You would place the event handler functions before the NPC is placed in Workspace.
dont put the script on the dummy, because if the dummy gets destroyed ( falls into the void ), The script gets destroyed too
local Time = script.RESPAWN_TIME.Value
local oldDummy = nil -- the first dummy ( change this )
local newDummy = nil -- a clone of the dummy
local function SetDummy(Dummy)
Dummy:WaitForChild("Humanoid").Died:Once(function()
task.wait(Time)
local new = newDummy:Clone()
new:MakeJoints()
new.Parent = workspace
Dummy:Destroy()
Dummy = new
SetDummy(Dummy)
end)
end
SetDummy(oldDummy)
Alright, so in your spawn code you would need to put in something like this:
function spawn()
-- All your current spawn code here...
local backupDummy = dummy:Clone() -- Assuming dummy is the name of your NPC variable
local function respawn()
dummy = backupDummy
dummy.Destroying:Connect(respawn)
dummy.Parent = workspace
backupDummy = dummy:Clone()
end
dummy.Destroying:Connect(respawn)
end
local Time = script.RESPAWN_TIME.Value
local currentDummy = nil -- the first dummy ( change this )
local newDummy = nil -- the dummy to clone
local function Respawn(Dummy)
task.wait(Time)
local new = newDummy:Clone()
--new:MakeJoints()
new.Parent = workspace
Dummy:Destroy()
Dummy = new
return Dummy
end
local function SetDummy(Dummy)
local DiedConnection = Dummy:WaitForChild("Humanoid").Died:Once(function()
SetDummy(
Respawn(Dummy)
)
end)
repeat task.wait() until not Dummy or not Dummy.Parent
DiedConnection:Disconnect()
SetDummy(
Respawn(Dummy)
)
end
SetDummy(currentDummy)
currentDummy - workspace or something else my mars
newDummy - somewhere to store that dummy like ReplicatedStorage
script - somewhere except the dummy or currentdummy