I tried different methods, but can’t seem to get it to work how i want it to.
Yes, I can put a respawn script in every npc, but that seems so inefficient and a pain if u got to edit.
What I tried was to make 1 script and make it loop through a folder where all my npcs are in.
as for the script:
for _,v in pairs(workspace.Animals:GetDescendants()) do
if v:IsA("Folder") then
continue
end
if v:IsA("Script") then
continue
end
if v:IsA("Model") then
local humanoid = v:FindFirstChild("Humanoid")
humanoid.Died:Connect(function()
game.Debris:AddItem(v, 1)
warn(v, "Died, respawning")
local serverStorage = game:GetService("ServerStorage")
local animalClone = serverStorage.Animals[v.Name]:Clone()
wait(1)
animalClone.Parent = v.Parent
if animalClone.Name == "Dog" then
animalClone:FindFirstChild("Humanoid").MaxHealth = 30
print(animalClone.Name.."'s health set to 30.")
end
end)
end
end
This works how I want it to, but only once.
Anyway how can make it like that, but make it actually work more than once?
Or if you have a better way, please I’m happy to hear it.
Believe it or not, been at this for 6 hours.
I also tried cloning the script to every npc, but same problem…works only once.
I feel like this is a simple solution and I’m just making it hard for myself.
while that would work, I don’t like the idea of every npc having a respawn script. If you have to edit something, u gotta edit every single script.
Unless I try cloning again, mhm…Cant believe im struggling so hard with it
I usually check whenever the NPC’s Humanoid dies. Then you can simply create a Clone of it and Parent it to workspace. (Assuming the NPC is inside ReplicatedStorage/ServerStorage)
This mostly depends on the way you scripted the NPC. If it has a Script that Controls it (and it is inside the NPC’s Model) you can simply disable the Script and enable it again whenever you Clone the NPC.
As long as you have the NPCs organised in some folder you can do something akin to the following.
local npcs = workspace.NPCFolder
for _, npc in ipairs(npcs:GetChildren()) do
local npcHuman = npc:FindFirstChildOfClass("Humanoid")
if npcHuman then
npcHuman.Died:Connect(function()
task.wait(1)
local npcClone = npc:Clone()
npcClone.Parent = npcs --Move clone into NPC folder.
npc:Destroy() --Destroy dead NPC.
end)
end
end
You’d create the same connections on the cloned NPC’s humanoids as well.
local npcs = workspace.NPCFolder
for _, npc in ipairs(npcs:GetChildren()) do
local npcHuman = npc:FindFirstChildOfClass("Humanoid")
if npcHuman then
npcHuman.Died:Connect(function()
task.wait(1)
local npcClone = npc:Clone()
npcClone.Parent = npcs --Move clone into NPC folder.
npc:Destroy() --Destroy dead NPC.
end)
end
end
npcs.ChildAdded:Connect(function(npc)
local npcHuman = npc.Humanoid
npcHuman.Died:Connect(function()
task.wait(1)
local npcClone = npc:Clone()
npcClone.Parent = npcs --Move clone into NPC folder.
npc:Destroy() --Destroy dead NPC.
end)
end)
Ok, with your help I got it to work. Had to change the code a little bit, but it works now.
local npcs = workspace.Animals.Dog
for _, npc in ipairs(npcs:GetChildren()) do
local npcHuman = npc:FindFirstChild("Humanoid")
if npcHuman then
npcHuman.Died:Connect(function()
task.wait(1)
local npcClone = game:GetService("ServerStorage").Animals.Dog:Clone()
npcClone.Parent = npcs
npc:Destroy()
end)
end
end
npcs.ChildAdded:Connect(function(npc)
print(npc)
local npcHuman = npc.Humanoid
npcHuman.Died:Connect(function()
task.wait(1)
local npcClone = game:GetService("ServerStorage").Animals.Dog:Clone()
npcClone.Parent = npcs
npc:Destroy()
end)
end)
I applied this script to my NPCs… and it only clones them once… and the clone doesnt behave like the original one, just sits dormant. I also keep an exact copy in ServerStorage.