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

How can I test for when two parts touch?

Asked by 6 years ago

I tried googling this but all that came up was if the player touched the part. Can somebody show how to do this?

0
:GetTouchingParts() greatneil80 2647 — 6y
0
how would I use it? barrettr500 53 — 6y
0
If you connect to .Touched it returns the part that touched it. exxtremestuffs 380 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
local part = script.Parent
part.Touched:connect(function()
    local touching = part:GetTouchingParts()
    for parts,touchingpart in pairs (touching) do
        print(touchingpart.Name)
    end
end)

This script prints the names of all parts that touch the part

local part = script.Parent
part.Touched:connect(function()
    local touching = part:GetTouchingParts()
    if #touching == 2 then
        --script here
    end
end)

this script executes your code when 2 parts touch the part

this sometimes bugs so i would prefer to use this:

local part = script.Parent
wait ()
while true do
    wait ()
    local touching = part:GetTouchingParts()
    if #touching == 2 then
        --script here
    end
end)

Ad

Answer this question