Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to detect all the parts that is touching another part?

Asked by 3 years ago

To make it more clear, I want to print the name of every part that's touching a part with a touch listener at the same time. Here is my code:

game.Workspace.Part.Touched:Connect(function(a)
    print(a.Name)
    game.Workspace.Part.CanTouch = false
    wait(0.5)
    game.Workspace.Part.CanTouch = true
end)

This code prints out the same part over and over again. It could be because the touch listener only has 1 parameter to detect just 1 part. So must I detect collision another way to print out all of the colliding parts, like raycasting?

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

This might be of use to you. This returns a table of all touching parts which include all BaseParts that either have cancollide true OR cancollide false with a touchInterest.

Incase your afraid it won't get all the parts because of properties, just add a touch interest to the part you want to get touching parts from. Then you can stop the interest after getting touching parts, this way, you have all the possible touching parts and the touch interest is gone afterwards which is good for memory. Like this

Connection = Part.Touched:Connect(function()end) -- adds touch interest

TouchingParts = Part:GetTouchingParts() -- sets variable to table of all touching parts

Connection:Disconnect() -- rids the parts touch interest to save memory so it doesn't keep track of something that's not used.

I hope this helps :3

0
Thanks a lot, I've never heard of that function before tuanorn 6 — 3y
Ad

Answer this question