while wait() do
if humanoid <= 0 then
local npc = game.ServerStorage.NPC
npc:Clone().Parent = game.Workspace
This will check if the npc’s health reached 0 and if it does, it will clone a copy of it into the workspace. Make sure to put the copy in ServerStorage.
No need to do a while loop when Humanoid.Died exists.
Simply change that code to
-- Assuming the script is inside your NPC
local npc = script.Parent
local hum = npc:WaitForChild("Humanoid")
local copy = npc:Clone() -- credit to @RoBoPoJi for mentioning
hum.Died:Connect(function() -- Connect
local klone = copy:Clone()
klone.Parent = workspace -- Clone and parent
end)
Your script clones the dead npc. The respawning should be done by cloning an npc model from serverstorage, like in @StarBoythehero123’s script. But yes, you are right, humanoid.died is better for this than checking health in a loop.
Also, @StarBoythehero123, in your script the value of the humanoid variable (which refers to the humanoid’s health) is only set once. So the check in the if statement will always have the same result. If you wanted to check the health in a loop, you should have the humanoid object set as the value of humanoid and do
if humanoid.Health == 0 then -- Humanoid.Health can't be smaller than 0, so it's not necessary to have <=.
-- code here
end
this is frustrating i cloned it and moved it into the server but it didnt replicate to the client it is visible from the server but invisible to the client
here is the code
-- // services \\ --
local CollectionService = game:GetService("CollectionService")
local NPCRootParts = CollectionService:GetTagged("NPCRootParts")
local ServerStorage = game:GetService("ServerStorage")
local NPCFolder = ServerStorage.NPC
local StartingZone = NPCFolder.StartingZone
local Villager = StartingZone.Villager
-- Debounce
local DAMAGE_DEBOUNCE = false
-- loop through if touched then damage them
for _, NPCRootPart in pairs(NPCRootParts) do
if NPCRootPart:IsA("Part") then
local Humanoid = NPCRootPart.Parent.Humanoid
if Humanoid then
Humanoid.Died:Connect(function()
Villager:Clone().Parent = workspace.MobHolder.StartingZone
NPCRootPart.Parent:Destroy()
end)
end
end
end