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!
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.