Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I have multiple buttons activate this script?

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

✓ Accepted answer
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)

1 comment

0

Thanks so much, I have no idea how I didn't come up with this :P

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

This discussion is preserved as it was. New answers, comments, and votes are closed.