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 make buttons work individually while using a for loop?

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

✓ Accepted answer

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

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