How to detect when a object gets added to a folder

I need help with making the script detect when a new dummy is added to the folder as well, not just first when the dummy folder is made. This is how to do it but I don’t know how to make it work with this script. Detecting when a object gets added to a folder

How do I do this?

This script is in ServerStorageScripts as a Server Script

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)
		task.wait(0.4)
		HeadUI.Redbar.Size = UDim2.new(pie, 0, 1, 0)
	end
	Humanoid.HealthChanged:Connect(updateHealth)
end
1 Like

Instance.ChildAdded

Scripts do not run in ServerStorage, and should be in ServerScriptService instead.

local folder = game.Workspace.Dummies

function dummyAdded(dummy)
	-- Verify that object being handed is a dummy and not something else
	if dummy.Name ~= 'Dummy' then return end

	-- todo: dummy logic here
	print('Dummy Added!')
end

-- Listen for new dummies being added
folder.ChildAdded:Connect(dummyAdded)
-- Hookup existing dummies in the folder
for _,dummy in folder:GetChildren() do
	dummyAdded(dummy)
end
1 Like

I tried the script and it did not work and I tried this and it did not work

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)
		task.wait(0.4)
		HeadUI.Redbar.Size = UDim2.new(pie, 0, 1, 0)
	end
	Folder.ChildAdded:Connect(function(child)
		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)
		task.wait(0.4)
		HeadUI.Redbar.Size = UDim2.new(pie, 0, 1, 0)
	end)
	Humanoid.HealthChanged:Connect(updateHealth)
end
local Folder = game.Workspace.Dummy

local function bindDummy(EachNPC)
    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)
    
    local 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)
        task.wait(0.4)
        HeadUI.Redbar.Size = UDim2.new(pie, 0, 1, 0)
    end
    
    Humanoid.HealthChanged:Connect(updateHealth)
end

-- Bind all existing dummies
for _, EachNPC in pairs(Folder:GetChildren()) do
    bindDummy(EachNPC)
end

-- Bind all future dummies
Folder.ChildAdded:Connect(bindDummy)

That works amazing Thank You!!!

1 Like

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