Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Using Region3 to replace Touched?

I have a problem with the Touched event ignoring Anchored parts, I'm making a barrier which moves things to the outer walls. For now, it just teleports parts to 0,0,0 as it's an easy way to test it.

barrier = script.Parent

function onTouch(hit)
	local h = hit.Parent
	if h == nil then return end
	if h == workspace then
		hit.CFrame = CFrame.new(0,0,0)
	else
		local part = h:findFirstChildOfClass("Part")
		if part ~= nil then
			part.CFrame = CFrame.new(0,0,0)
		end		
	end
end
barrier.Touched:connect(onTouch)

It works on any part unless it is anchored, and that's where the issue is. Because the Touched event doesn't work on anchored parts, it allows anything that uses anchored parts to kill to just bypass it.

For example, Scroll of Sevenless (a Roblox gear) creates several parts which explode, and those parts are anchored so they end up not getting repositioned.

I was wondering if I could use Region3 and set its boundaries to be equal to the barrier part's dimensions and then reposition anything within those boundaries? - If that won't work, please tell me any solutions you know of.

1 answer

✓ Accepted answer

Yes, you could use Region3 to essentially replace .Touched, but since Region3's don't rotate, you'd need to do some math on your part to set up the hitboxes, which may actually be slower than some of the built in roblox C functions. It's still viable though if desired.

A good alternative to this is :GetTouchingParts(), which returns a list of touching parts, working for anchored parts, which .Touched lacks compatibility for, provided you actually set up an empty touch interest via Part.Touched:Connect(function() end) for the sole purpose of adding a touched interest. This method is probably the easiest to set up.

game.Workspace.Part.Touched:Connect(function() end)

while game:GetService("RunService").Stepped:Wait() do
	for _,v in pairs(game.Workspace.Part:GetTouchingParts()) do
		--Run Code with v being the parts touching.
	end
end

Another possible solution regarding this is to actually set up raytracing as a form of hit detecting. This essentially works by creating an array of points on the object, often times done through attachments, and raycasting from the point to where it was previously at an older time interval, while detecting parts within the path of the ray.

Additionally, if I missed anything, please let me know in the comments below.

6 comments

0

Thank you very much, I'm going to give GetTouchingParts() a try because raytracing is probably too complicated for me, and sounds laggier.

0

Alright, if it worked, please accept the answer posted! Really helps me out with something.

0

Yes, it worked! Thank you so much.

0

Np!

0

Hey. Is there any way to opt an individual part out of this process without breaking it? Because if it touches the Baseplate or its child part which is meant to give it an outline it teleports those too.

0

Ya, simply do an if v.Name ~= "Baseplate" then in it, it should exclude anything by the name of baseplate, this can be done repeatedly as a sort of filter.

This discussion is preserved as it was. New answers, comments, and votes are closed.