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

How do I make buttons work individually while using a for loop?

Asked by
d3nton 19
2 years ago

I'm trying to make a gui that provides admin commands, but I don't want to copy the code to make the buttons, so I decided to use a for loop. However, if a button is pressed, the script will not know which button was pressed. So could someone please show me how to make the buttons work independently while still using a for loop?

Options = {
    "Fly",
    "ForceField",

}

for _, v in pairs(Options) do
    local TextButton = Instance.new("TextButton", ScreenGUI)
    TextButton.Text = v
    TextButton.BackgroundColor3 = Color3.fromRGB(56, 56, 56)
    TextButton.Font = Enum.Font.SourceSansItalic
    TextButton.TextSize = 20
    TextButton.TextColor3 = Color3.fromRGB(255, 255, 255)

    local UICorner = Instance.new("UICorner", TextButton)
    UICorner.CornerRadius = UDim.new(0, 5)

    local UIGradient = Instance.new("UIGradient", TextButton)
    UIGradient.Rotation = 90
    UIGradient.Color = ColorSequence.new({
        ColorSequenceKeypoint.new(0, Color3.fromRGB(255,255,255)),
        ColorSequenceKeypoint.new(1, Color3.fromRGB(197,197,197))
    })
end

1 answer

Log in to vote
0
Answered by 2 years ago

I'm assuming this is what you're looking for?

for Index, Buttons in pairs(Element:GetChildren()) do
    if Buttons:IsA("TextButton") then
        Buttons.MouseButton1Click:Connect(function()
            if Buttons.Name == "Bye" then
                print("Bye World!")
            elseif Buttons.Name == "Hello" then
                print("Hello World!")
            end
        end)
    end
end
Ad

Answer this question