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

How do I have multiple buttons activate this script?

Asked by 4 years ago

This is my script, and Button activates it. However, I have two other buttons, which are Button2 and Button 3. I would like to know how to have all three buttons be able to activate this script? Thanks!

script.Parent.Button.ClickDetector.MouseClick:connect(function()
    if script.Parent.Alternate.CanCollide == false then
        script.Parent.Alternate.CanCollide = true
        script.Parent.Straight.CanCollide = false
        script.Parent.AlternateIndicator.BrickColor = BrickColor.new("Lime green")
        script.Parent.StraightIndicator.BrickColor = BrickColor.new("Really red")
    else
        if script.Parent.Straight.CanCollide == false then 
            script.Parent.Straight.CanCollide = true
            script.Parent.Alternate.CanCollide = false
            script.Parent.AlternateIndicator.BrickColor = BrickColor.new("Really red")
            script.Parent.StraightIndicator.BrickColor = BrickColor.new("Lime green")
        end
    end
end)

2 answers

Log in to vote
1
Answered by 4 years ago
function activate()
        if script.Parent.Alternate.CanCollide == false then
        script.Parent.Alternate.CanCollide = true
        script.Parent.Straight.CanCollide = false
        script.Parent.AlternateIndicator.BrickColor = BrickColor.new("Lime green")
        script.Parent.StraightIndicator.BrickColor = BrickColor.new("Really red")
    else
        if script.Parent.Straight.CanCollide == false then 
            script.Parent.Straight.CanCollide = true
            script.Parent.Alternate.CanCollide = false
            script.Parent.AlternateIndicator.BrickColor = BrickColor.new("Really red")
            script.Parent.StraightIndicator.BrickColor = BrickColor.new("Lime green")
        end
    end
end

Button1.MouseClick:Connect(activate)
Button2.MouseClick:Connect(activate)
Button3.MouseClick:Connect(activate)
0
Thanks so much, I have no idea how I didn't come up with this :P rubiksmaster301 17 — 4y
Ad
Log in to vote
0
Answered by
JakyeRU 637 Moderation Voter
4 years ago
Edited 4 years ago

Hello. The simplest way is to create a function with your code and activate it every time a button is pressed. Since you have only 3 buttons, you could do:

Button1.MouseClick:Connect(FunctionName)
Button2.MouseClick:Connect(FunctionName)
Button3.MouseClick:Connect(FunctionName)

But, let's say you have multiple buttons, you should use a for loop. Here is an example:

You should put all the buttons in a Folder to keep it more organized like in the image below: Click Here

Then, with a Script, you could get all descendants, check if they are a MouseDetector and listen to their event.

local Buttons = game.Workspace:WaitForChild("Buttons"):GetDescendants()

function FunctionToRun()
    print("Activated!")
end

for _, v in pairs(Buttons) do
    if v:IsA("ClickDetector") then
        v.MouseClick:Connect(FunctionToRun)
    end
end

Now, it will run the function FunctionToRun and will print Activated each time you click a button.

Please read these articles:

Instance:IsA

For Loops

Instance:GetDescendants

Functions

Click Detector

Answer this question