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

How to make an object work only if touching another object?

Asked by 6 years ago

Hello. I'm trying to make a script where it only works if it's touching another specific object. The current script I use is

 local function onTouch(hit)
    if (hit.Parent.Parent.Parent) then
        print(hit)
    script.Parent.Parent.Object1.AlignPosition.Enabled = true
    wait(1)
    script.Parent.Parent.Object1.AlignPosition.Enabled = false
    end
end

script.Parent.Touched:connect(onTouch)

I've tried doing

 local function onTouch(hit)
    if (hit.parent.findFirstChild("Object2")) then
        print(hit)
    script.Parent.Parent.Object1.AlignPosition.Enabled = true
    wait(1)
    script.Parent.Parent.Object1.AlignPosition.Enabled = false
    end
end

script.Parent.Touched:connect(onTouch)

but it doesn't work. When I use the first script, anything touching Object1 activates AlignPosition, but I only want it that when it touches Object2 it activates AlignPosition. Any help is appreciated. Thanks!

1 answer

Log in to vote
0
Answered by 6 years ago
 local function onTouch(hit)
    if hit.Name = "Object2" then
        print(hit)
    script.Parent.Parent.Object1.AlignPosition.Enabled = true
    wait(1)
    script.Parent.Parent.Object1.AlignPosition.Enabled = false
    end
end

script.Parent.Touched:connect(onTouch)

That should work

0
Yep, it works. Thank you so much! ROCKANDROLL572 12 — 6y
0
np User#20388 0 — 6y
0
btw, your way could also work but you used "." and not this ":" User#20388 0 — 6y
Ad

Answer this question