How can I detect when an item is added to a region 3

Hello, so i’m making a hitbox module. I’m wondering if theres any way I can see if something is added to the region 3. If you have any questions then feel free to say below, Any help is appreciated

2 Likes

You can update the existing table of objects inside the Region3 every frame, and just before you update it leave a reference for the old table. Iterate through the new table, and find if the current item you are iterating through doesn’t exists in the old table, if it doesn’t exist, you know it was added, otherwise, not.

3 Likes

I will try this. Thank you for the idea

1 Like

this works. But may I ask one thing, how can I make this faster? This is my script:

while wait() do
	local Hitbox = workspace:GetPartsInPart(script.Parent, OverlapParams.new())
	wait(0.5)
	local newhitbox = workspace:GetPartsInPart(script.Parent, OverlapParams.new())
	for i,v in pairs(newhitbox) do
		if not table.find(Hitbox, v) then
			print('Added')
		end
	end
end

I am pretty sure that you can run this every frame (RenderStepped) to increase it’s speed.
Additionally, I do not think you need to get the region twice, talking performance wise.
It is better that you leave a variable called ‘lastHitbox’ outside of the while loop, and in the beginning of every loop you can reference it as ‘oldHitbox’ and then update the ‘lastHitbox’ based on the current region.

1 Like

Bro, I love you thank you so much. This works really well. I can’t wait to add this to my module!!!
btw this is my script to anyone wondering

local OldHitbox = workspace:GetPartsInPart(script.Parent, OverlapParams.new())
local runservice = game:GetService("RunService")
runservice.Stepped:Connect(function()
	local newhitbox = workspace:GetPartsInPart(script.Parent, OverlapParams.new())
	for i,v in pairs(newhitbox) do
		if not table.find(OldHitbox, v) then
			print('Added')
		end
	end
	OldHitbox = newhitbox
end)


1 Like

One question, how can I make it fire once and not multiple times? I don’t want it to print it multiple times

Edit: nvm, i just forgot to add the not in my if statement.

1 Like