Help turning something into a function

Its not listening to one event is better for performance than listening for 2 events

How do I make a master script that does that.

the scripts are located in a health gui for the NPCs

If they are all in a folder, loop through each Object like:

local Folder = nil --Put pathway to folder here

for i, EachNPC in Folder:GetChildren() do
--Code here
end

This is an example about how we can use this instead of having many different scripts instead of one

where do i put the script? In the fold?

Almost anywhere really. Though probably serverscriptservice would be most logical for me

I have four types dummies (DummyA, DummyB ,DummyC ,DummyD) in the folder
how do I get to all of there humanoids in the same script

is all the Dummy’s Models at once. Because it loops through all of them. So just EachNPC:FindFirstChild(“Humanoid”)

Ok I got it to work but a error comes up
“Workspace.Dummy.HealthManager:5: attempt to index nil with ‘HeadUI’”

The script does what I want but I don’t know how to remove this error and why it’s it came up

local Folder = script.Parent

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

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