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)
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)
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: