How to fix this script

After the NPC dies I want the NPC to destroy itself and make it to what it was

What I want after death of the NPC

What happens but should not happen

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

How do I fix this?

no idea if this would work but try this:

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?

1 Like

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

1 Like

The script did not work. The NPC health ui said N/A after respawned

1 Like

I may be misunderstanding, but is the issue after the Dummy respawns, or when you originally play?

1 Like

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
1 Like

It happens after the Dummy respawns

1 Like

You need to detect when a new dummy is added to the folder as well, not just first when the dummy folder is made.

1 Like

How would I be able to do that

Dummy.ChildAdded:Connect(function()

where do I put it. Do I make another script or put it in the same script

Try this;

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)

That did not work. When I started the game the the NPCs’ health ui said N/A

Okay, is there any other scripts that are attached to this NPC? Also, if possible, can you create a test world which I can interact with?

How do I send it just to you and not this chat

Private message me, but I may not respond till tommorow.

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

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.