How can I get the full table outside of this hit connection?

local partsHit = {}

-- listen for contact
explo.Hit:Connect(function(part, distance)
	local parentModel = part.Parent
	local car = parentModel:FindFirstAncestor("Cars")
	if car then
		local distanceFactor = math.clamp(1 - (distance / explo.BlastRadius) * 10, 0, maxDamage) 		
		table.insert(partsHit, {part = part, damage = distanceFactor})
	end
end)
		
print(partsHit)
PhysModule.DealDamage(partsHit)

Trying to get the partsHit table results in an empty table after the connection.
I believe the issue is me trying to call for the table before the hit gets to add any parts to the table, but I do not know any way to get the table and its contents after everything has been added to it

Hopefully its something simple im missing, anyone have any ideas?

1 Like

It is. The event is listening and it isn’t yielding the script (so the print statement can still run).

(Wish we had built in Promises)

If that snippet you provided is the only code in the script, you could yield the rest of the script by using a repeat until loop

Also this kinda depends. Do you want the table to be printed out AFTER EVERY HIT EVENT ENDS. Or something else

listen to chexburger’s post

Anything outside :Connect will run independently and not when the part is hit. You will need to deal damage inside your event callback.

2 Likes

I have a seperate module script handling Damage and I would prefer to send only one large table of parts at a time instead of sending say 50 individual parts.
This is a very important part of my game and it needs to be performant.

I may be able to simply wait for all the parts, not the cleanest but if it works it works ig :sweat_smile:

1 Like

Ah, unfortunate. I appreciate your response ill try to get something performant going on in there. :+1:

This is not possible with events. The Hit event will fire for every individual part. Unless the damage is triggering other explosions, the performance improvement from using a table is miniscule if anything.

If you want to avoid using hit detection like that, you can create a sphere when the explosion occurs and utilize GetPartsInPart then delete the sphere. This will give you a table of all parts that would be hit by the explosion, assuming you match the sphere’s size with your explosion’s radius.

2 Likes