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

How would I make a set of buttons in which only one can be selected at a time?

Asked by
SuperFryX 130
8 years ago

EDIT (what's with the downvotes? Please read the last sentence, I'm not requesting for a script...)

I'm about to go and script a a coffee machine where you select the coffee type in a column of buttons and the flavoring in another column, and the selected buttons would be highlighted in green. But I only want one button to be selected at a time in a column. I've been brain storming ways on how to do this and all my ideas feel too inefficient. How would I go about scripting this? I don't need you to write any scripts of any kind, I would just like to know the basic layout so I know how to organize it as I'm not that great with script organization.

1 answer

Log in to vote
0
Answered by 8 years ago

Hey there!

This is just one method I have thought of while reading your question.

While I will not be providing any scripts, take these into consideration:

  • Have a BoolValue inside each button. Use a Changed event to connect to the BoolValue, and whenever it is changed, change the colors. Example:
script.Parent.Selected.Changed:connect(function (onChange)
    if script.Parent.Selected.Value then
        -- Color for selcted
    else
        -- Default color
    end 
end)

You could then have a master script, which would check all the values, and toggle them

An example:

local Buttons = {
script.Parent.Button1,
script.Parent.Button2,
}

function CheckButtons()
    for _,Button in pairs(Buttons) do
        if Button.Selected.Value then
            return true
            Button.Selected.Value = false
        end
    end

    return false
end

for _,Button in pairs (Buttons) do
    Button.MouseButton1Down:connect(function (click)
        CheckButtons()
        Button.Selected.Value = true
    end)
end
Note: I've done no testing of this whatsoever. I am actually in economics class while writing this, so please excuse basic mistakes.
Ad

Answer this question