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?
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
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.
--// 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
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
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.