Help with understanding :GetTouchingParts()

Hello, I want to use :GetTouchingParts(), but I don’t understand how to use it. I would like some examples. Thank you.

2 Likes

So basically :GetTouchingParts() is a function that returns a table of all parts that are touching your basepart, this table acts like any other table. So for example you could do:

local basePart = workspace.Part

local touchingPartsTable = basePart:GetTouchingParts()

for i,v in pairs(touchingPartsTable) do
print(v)
end

Since it acts like any other table, you could iterate through it like I did, this should print all the parts that are touching(intersecting) your basePart.

3 Likes

Ok, thanks. Also, to see if a part is in the table, do I do this?

local randomPart = game.Workspace.randompart

if randomPart = touchingPartsTable then

end)

Not quite, so going off of the code I sent earlier you could do:

if table.find(touchingPartsTable, workspace.Part1) then
print("yes")
end

Basically if statements always look for two things; if something is true or if it exists(not nil). randomPart does not equal the table “touchingPartsTable” therefore nothing will happen. Also make sure you’re using the operator == when checking if something is true or false.

1 Like

Thank you, your information helped me a lot. Do you need a credit for my project?

Nope, just helping out wherever I can

2 Likes