ChildAdded not firing when I add a child

I am trying to connect a ChildAdded event to a part created by one module in a second module. See the code:

    local newHitbox = powerUtils.SimpleHitbox(initPlayer,boxParams,hitParams)

    print(newHitbox) -- just to prove its there, it is
    newHitbox.ChildAdded:Connect(function(child)
            print("we hit it!: ",child.Value)
    end)

so the function I am calling, powerUtils.SimpleHitbox() is in another module, and I have a ChildAdded event in that function and it works perfectly. The SimpleHitbox function returns the hitbox object and I have a print there to prove it exists, its parented to workspace. This is all on the server.

So the issue is, in THIS module the ChildAdded event will never fire, and I am verifying in the Explorer that children are being added, I can see them being added and yet the event just wont fire.

I feel like I am missing something very obvious, but I’m stumped. Anyone see what up with this?

2 Likes

Hmm, try printing the connection itself just to make sure that exists, it should say Connection in the output:

print( newHitbox.ChildAdded:Connect(function(child)
        print("we hit it!: ",child.Value)
end) ) -- should print Connection
1 Like

this does print “connection”

this is how my code looks right now:

    local newHitbox = powerUtils.SimpleHitbox(initPlayer,boxParams,hitParams)

    newHitbox.ChildAdded:Connect(function(child)
        if child.Name == "CharacterHit" then
            print("we hit it!: ",child.Value)
        end
    end)

    print( newHitbox.ChildAdded:Connect(function(child)
        print("we hit it!: TEST ",child.Value)
    end) ) -- should print Connection

Really cant understand what happening

EDIT: I added a print outside the child.Name check just to see if that would print, it does not

    newHitbox.ChildAdded:Connect(function(child)
        print("boop") -- new print here
        if child.Name == "CharacterHit" then
            print("we hit it!: ",child.Value)
        end
    end)

    print( newHitbox.ChildAdded:Connect(function(child)
        print("we hit it!: TEST ",child.Value)
    end) ) -- should print Connection
1 Like

Can you show the code for the ‘powerUtils’ module and the code that is supposed to add items to ‘newHitbox’?

1 Like

If I had to take a quick guess on this, perhaps you are overwriting your newHitBox and then adding a child to that instance of it. You still :connect to this instance but no child is being added to it.

Just a guess and an easy way to check is after defining newHitBox in that module, simply Instance.new something and parent it to that newHitBox. If it fires from parenting then that’s probably what’s happening.

1 Like

sure its a bit long but here we go:

--// SimpleHitBox -- just creates a simple hitbox - HIS HITBOX CAN ONLY HIT A HUMANOID ONCE PER INSTANCE
-- boxParams define the box itself such as size
-- hitParams define what will be be sent to PowerService:RegisterHit() when something is hit
function PowerUtils.SimpleHitbox(initPlayer,boxParams,hitParams)

	local hitBox = Instance.new("Part")

	-- set some defaults but we can override them with boxParams
	hitBox.Color = Color3.new(255/255, 102/255, 204/255)
	hitBox.Massless = true
	hitBox.CanCollide = false
	hitBox.Anchored = true
	hitBox.Parent = workspace.ServerHitboxes[initPlayer.UserId] -- parented to the initPlayer folder, this is so we can find the owner if we ever need to

	-- set anything from boxParams, this override defaults, OBVIOUSLY lol
	for key,value in pairs(boxParams) do
		hitBox[key] = value
	end

	-- a list of characters already hit, these get added in the Touched
	local hitList = {} 

	-- get all touching parts and hit them, this allows us to hit anything that was inside the hitbox when it spawned
	local connection = hitBox.Touched:Connect(function() end)
	local results = hitBox:GetTouchingParts()
	connection:Disconnect()

	for _,hit in pairs (results) do
		if hit.Parent:FindFirstChild("Humanoid") then

			-- check if this character was already hit
			local characterHit = hit.Parent
			local canHit = true
			for alreadyHit,_ in pairs(hitList) do
				if alreadyHit == characterHit then
					canHit = false
					break
				end
			end

			-- now add the character to the table, this produce no duplicates
			hitList[characterHit] = true

			-- check if this is the initPlayer, set canHit to false if it is
			for _, player in pairs(Players:GetPlayers()) do
				if player.Character == characterHit then
					canHit = false
				end
			end

			-- do the hit if canHit is true
			if canHit == true then
				Knit.Services.PowersService:RegisterHit(initPlayer,characterHit,hitParams)
				local newValueObject = Instance.new("ObjectValue") -- will store a character
				newValueObject.Name = "CharacterHit"
				newValueObject.Parent = hitBox
				newValueObject.Value = characterHit
			end

		end
	end

	
	-- the Touched event for new hits
	hitBox.Touched:Connect(function(hit)

		local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
		if humanoid then

			-- check if this character was already hit
			local characterHit = hit.Parent
			local canHit = true
			for alreadyHit,_ in pairs(hitList) do
				if alreadyHit == characterHit then
					canHit = false
					break
				end
			end

			-- now add the character to the table, this produce no duplicates
			hitList[characterHit] = true

			-- check if this is the initPlayer, set canHit to false if it is
			for _, player in pairs(Players:GetPlayers()) do
				if player.Character == characterHit then
					canHit = false
				end
			end

			-- do the hit if canHit is true
			if canHit == true then
				Knit.Services.PowersService:RegisterHit(initPlayer,characterHit,hitParams)
				local newValueObject = Instance.new("ObjectValue") -- will store a character
				newValueObject.Name = "CharacterHit"
				newValueObject.Parent = hitBox
				newValueObject.Value = characterHit
			end
		end
	end)

	return hitBox
end
1 Like

hrm, good idea for a test. will try it. I dont THINK thsats what happenning though but i am so stumped right now, i will try anything!

1 Like

So adding instance from the first script does indeed fire the ChildAdded event what not firing is the children added by the hitbox script.

here is my code as is right now

    local newHitbox = powerUtils.SimpleHitbox(initPlayer,boxParams,hitParams)

    --Debris:AddItem(newHitbox,.5)

    newHitbox.ChildAdded:Connect(function(child)
        print("we hit it!: ",child.Name)
    end)

    wait(1)
    local newValueObject = Instance.new("StringValue")
    newValueObject.Name = "CharacterHit_TEST"
    newValueObject.Parent = newHitbox
    newValueObject.Value = "TEST"

    wait(1)
    local newPart = Instance.new("Part") -- will store a character
    newPart.Name = "Part_TEST"
    newPart.Parent = newHitbox

as proof that all the childen have been added, here is a screenshot of the hitbox in explorer
A Bizarre Timeline - Roblox Studio (gyazo.com)

the script was yielding in an unexpected place and when it returned the hitPart, the child was already there and so could not fire the ChildAdded event

i added s simple wait(.1) and that fixed it

Yep, yielding would be a culprit in that scenario. Glad you have it all sorted!

actually! i realized i was parenting the object before setting the value, didnt need any waits after all, just made sure value was set before parenting.

thats a silly one for sure :stuck_out_tongue:

1 Like