This script is a server script in ServerStorageStripts
local Folder = game.Workspace.Dummy
for i, EachNPC in Folder:GetChildren() do
local Copy = EachNPC:Clone()
local NPC = EachNPC
local Humanoid
local BodyPart = EachNPC
for i,v in pairs(NPC:GetChildren()) do
if v:IsA('Humanoid') then
Humanoid = v
end
end
if Humanoid then
Humanoid.Died:Connect(function()
BodyPart.Head.HeadUI.Enabled = false
task.wait(5)
Copy.Parent = NPC.Parent
Copy:MakeJoints()
NPC:Destroy()
end)
end
end
local Folder = game.Workspace.Dummy
for i, EachNPC in pairs(Folder:GetChildren()) do
local Copy = EachNPC:Clone()
local NPC = EachNPC
local Humanoid
local BodyPart = EachNPC
for i,v in pairs(NPC:GetChildren()) do
if v:IsA('Humanoid') then
Humanoid = v
end
end
if Humanoid then
Humanoid.Died:Connect(function()
BodyPart.Head.HeadUI.Enabled = false
task.wait(5)
Copy.Parent = NPC.Parent
Copy:MakeJoints()
NPC:Destroy()
end)
end
end
also can you be a bit more clear as to what happens when the NPC dies please? is the health bar supposed to change red, or should the npc just not be there in general?
I will try the script. As the NPC takes damage the health ui show the health of the NPC. When the NPC dies the NPC fades away with particle effects (on a different script) and copies itself and destroys itself. After 5 seconds the NPC should “respawn” like nothing happened. That is what should happen to the NPCs
It might not be that script it bight be the NPC health ui script. Is there somthing wrong where the ui dose not reset after it respawns
local Folder = game.Workspace.Dummy
for i, EachNPC in Folder:GetChildren() do
local Head = EachNPC:FindFirstChild("Head")
local HeadUI = Head.HeadUI.UI.HealthUI
local Humanoid = HeadUI.Parent.Parent.Parent.Parent:FindFirstChild("Humanoid")
HeadUI.HealthNum.Text = math.floor(Humanoid.Health) .. " / " .. math.floor(Humanoid.MaxHealth)
function updateHealth()
HeadUI.HealthNum.Text = math.floor(Humanoid.Health) .. " / " .. math.floor(Humanoid.MaxHealth)
local pie = (Humanoid.Health / Humanoid.MaxHealth)
HeadUI.Healthbar.Size = UDim2.new(pie, 0, 1, 0)
end
Humanoid.HealthChanged:Connect(updateHealth)
end
local Folder = game.Workspace.Dummy
function updateHealth(HeadUI, Humanoid)
HeadUI.HealthNum.Text = math.floor(Humanoid.Health) .. " / " .. math.floor(Humanoid.MaxHealth)
local pie = (Humanoid.Health / Humanoid.MaxHealth)
HeadUI.Healthbar.Size = UDim2.new(pie, 0, 1, 0)
end
local function updateNpc(Head)
local HeadUI = Head.HeadUI.UI.HealthUI
local Humanoid = HeadUI.Parent.Parent.Parent.Parent:FindFirstChild("Humanoid")
HeadUI.HealthNum.Text = math.floor(Humanoid.Health) .. " / " .. math.floor(Humanoid.MaxHealth)
Humanoid.HealthChanged:Connect(updateHealth)
end
Folder.ChildAdded:Connect(function(child)
if child:FindFirstChild("Head") then
local head = child:FindFirstChild("Head")
updateNpc(head)
end
end)
local Folder = game.Workspace.NPC --Location of dummies
local function bindDummy(EachNPC) --Start fuction
local Head = EachNPC:FindFirstChild("Head") --Looks NPC Head
local HeadUI = Head.HeadUI.UI.HealthUI --Location of UI
local Humanoid = HeadUI.Parent.Parent.Parent.Parent:FindFirstChild("Humanoid") --Looks for Humanoid
HeadUI.HealthNum.Text = math.floor(Humanoid.Health) .. " / " .. math.floor(Humanoid.MaxHealth) --Math of NPC health
local function updateHealth() --start fuction
HeadUI.HealthNum.Text = math.floor(Humanoid.Health) .. " / " .. math.floor(Humanoid.MaxHealth) --Math of NPC health
local pie = (Humanoid.Health / Humanoid.MaxHealth) --Math of NPC health
HeadUI.Healthbar.Size = UDim2.new(pie, 0, 1, 0) --Moves Healthbar
task.wait(0.4) --Wait
HeadUI.Redbar.Size = UDim2.new(pie, 0, 1, 0) --Moves RedBar
end --End function
Humanoid.HealthChanged:Connect(updateHealth) --Connect function (updateHealth)
end --End function
for _, EachNPC in pairs(Folder:GetChildren()) do -- Bind all existing dummies
bindDummy(EachNPC)
end --End for
Folder.ChildAdded:Connect(bindDummy) -- Bind all future dummies