Child Added Dosent work

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

i want to detect that the child added in a folder so i add a health bar to it

  1. What is the issue? Include screenshots / videos if possible!

it dosent work

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

i tried many solutions on Dev Hub

i tried debugging and printing but it didnt do anything i feel like the child added isnt even firing

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Script (Not Done)

local MobModule = require(script.Mobs)
local SpawnBalloon = game.ReplicatedStorage:WaitForChild("SpawnBalloon")
local SpawnTower = game.ReplicatedStorage:WaitForChild("AddTower")
local Information = game.ReplicatedStorage:WaitForChild("MainGame"):WaitForChild("Information")
local GameStarted = game.ReplicatedStorage:WaitForChild("MainGame"):WaitForChild("GameStarted")
local Rounds = game.ReplicatedStorage:WaitForChild("MainGame"):WaitForChild("Rounds")
local OpenGui = game.ReplicatedStorage:WaitForChild("OpenGui")
local StartingCash = 100

local function GiveCash(Player, Amount)
	Player.PlayerData.Cash.Value = Player.PlayerData.Cash.Value + Amount
end

local function SetHealth(Char)
	local Hum = Char.Humanoid
	local GuiClone = game.ReplicatedStorage.HealthGui:Clone()
	GuiClone.Parent = Char


		--// Scripting HealthBar \\--

	local colorGreen = Color3.fromRGB(0, 255, 0)
	local colorRed = Color3.fromRGB(255, 0, 0)
	
	while wait() do
		local Health = Hum.Health
		local MaxHwalth = Hum.MaxHealth
			local Percentage = Health / MaxHwalth
		
		GuiClone.TextLabel.Text = GuiClone.Parent.Name..": "..Health.."/"..MaxHwalth
		GuiClone.Health.Size = UDim2.new(Percentage, 0, 1 ,0)
		GuiClone.Health.BackgroundColor3 = colorRed:Lerp(colorGreen, Health / MaxHwalth)
	end
end

for i = 10,0,-1 do
	Information.Value = "Game Starting In "..i
	wait(1)
	if i == 0 then
		local RandomPlayer = game:GetService("Players"):GetChildren()[math.random(1,#game:GetService("Players"):GetChildren())]
		print(RandomPlayer.Name)
		for i, v in pairs(game.Players:GetPlayers()) do
			GiveCash(v, StartingCash)
			
			if v.Name == RandomPlayer.Name then
				v.PlayerData.IsImposter.Value = true
				OpenGui:FireClient(v, v.PlayerGui.TowerGui)
			else
				OpenGui:FireClient(v, v.PlayerGui.BalloonGui)
			end
		end
		print("Finished with Players")
		GameStarted.Value = true
		coroutine.wrap(SetHealth)(workspace.Base)
		SpawnBalloon.OnServerEvent:Connect(function(Player, BalloonName)
			MobModule.Spawm(BalloonName)
		end)
		while wait(.25) do
			MobModule.Spawm("PinkBalloon")
		end
		workspace.Balloons.ChildAdded:Connect(function(Object)
			print("Object Spawned: ".. Object.Name)
			coroutine.wrap(SetHealth)(Object)
		end)
	end
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

If you spawned the balloons in line MobModule.Spawm("PinkBalloon") and MobModule.Spawm(BalloonName), the child added wouldn’t fire because the event was connected after the mobs were spawned in. (Child Added only detect instances that are added in, not the current instances)
You can fix it by connecting the event outside of the for loop or before the balloon spawns.
Also for your SetHealth() function, I dont suggest using while wait() do as it will run every frame and cause lag. Instead, use the event hum.HealthChanged to listen for health changes.

Ok let me change all the things

the game is less laggier but it still dosent fire

nvm it works the first solution you said didnt worked but the second worked

1 Like
while wait() do
	local Health = Hum.Health
	local MaxHwalth = Hum.MaxHealth
	local Percentage = Health / MaxHwalth

	GuiClone.TextLabel.Text = GuiClone.Parent.Name..": "..Health.."/"..MaxHwalth
	GuiClone.Health.Size = UDim2.new(Percentage, 0, 1 ,0)
	GuiClone.Health.BackgroundColor3 = colorRed:Lerp(colorGreen, Health / MaxHwalth)
end

The reason the second solution worked is because this is infinitely yielding code, its breaking condition will never evaluate to false (task.wait always returns a non-nil/non-false value), once the scope of this iteration structure is entered it is never exited meaning that any following code is not executed.