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

How would I use :GetTouchingParts() and check the names of the parts it returns?

How would I use :GetTouchingParts() and check the names of the parts it returns?

I have tried multiple things in ROBLOX Studio with :GetTouchingParts() , but I can't get it to work properly, and yes, my parts are all CanCollide-able.

The ROBLOX Wiki does not explain this that well, so I have yet to understand how I would use it.

Please note: I am not asking for anyone to create a script for me, all I want to know is how I would use :GetTouchingParts() and check the names of the parts it returns.

Any help is greatly appreciated!

1 answer

✓ Accepted answer

GetTouchingParts() returns a table of parts which are touching the part in question. The description from the wiki is as follows, "Returns a table of all CanCollide true parts that intersect with this part. If the part itself is CanCollide false, then this function will return an empty table. Parts that are adjacent but not intersecting are not considered touching."

Here's an example of how you would apply it; assume this is a Script inside of a Part:

local part = script.Parent;
part.Touched:connect(function() --// When the part is touched, call this function
	local touching = part:GetTouchingParts(); --// Get the table of touching parts.
	for _, p in next, touching do
		print(p.Name..' is touching '..part.Name..'!');
	end;
	print();
end;

This script would print the names of the touching parts as returned by part:GetTouchingParts(). Hope this helped.

Read more: For Loops & Anonymous Functions.

3 comments

0

Thanks!

0

No problem.

0

you made spaces before the beginn of a line, try pressing TUB at the beginnig of a line

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