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

How do I make a script run if it's touching a specific object?

Asked by 6 years ago

Hello. I'm trying to make a script that runs one thing if it's touching an object, and run another script if it isn't. Say A is the object I want to touch object B. I'm trying to make it so that when a button is clicked, if A is touching B, then run script 1. If A isn't touching B, run script 2. The current script i'm using is

  script.Parent.ClickDetector.mouseClick:connect(function()
    if Workspace.Part.Touched:connect(function() then
            script.Parent.Parent.A.AlignOrientation.Enabled = true
            script.Parent.Parent.A.AlignPosition.Enabled = true
            wait(2)
            script.Parent.Parent.A.AlignOrientation.Enabled = false
            script.Parent.Parent.A.AlignPosition.Enabled = false

    end)
    else
            script.Parent.Parent.A.AlignOrientation.Enabled = false
            script.Parent.Parent.A.AlignPosition.Enabled = false
            wait(2)
            script.Parent.Parent.A.AlignOrientation.Enabled = true
            script.Parent.Parent.A.AlignPosition.Enabled = true

end)

It doesn't do anything when clicked, so any help fixing this would be appreciated. Thanks!

1 answer

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

Try making a touch function in the object and make it check whether the other one is touching if it is, it will run one script else, it will run the other.

EDIT:

ClickDetector Script:

script.Parent.ClickDetector.MouseButton1Click:Connect(function()
    --enable the script in object A or B
end)

In Object A or B:

script.Parent.Touched:Connect(function(hit)
    if hit.Name == B then
        --enable said script here
else
        --enable other said script

end
end)

You can do the same script in B, but change == B to == A. Also, disable these scripts, so that they only run once the button is clicked.

0
I'm still a bit new at scripting, so how would you do that? Do you have any videos or forum links that can teach it? ROCKANDROLL572 12 — 6y
0
I'll give you an example. Conquesias 85 — 6y
Ad

Answer this question