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

How would i use buttons created in a for loop?

here is the script i am working with

local teamService = game:GetService("Teams")

local teamsTabel = {
	"Mailman",
	"Chef",
	"GameDev",
	"Jobless",
	"Child",	
	"Store Clerk"
}

for i, v in pairs(teamsTabel) do
	local newTeam = Instance.new("Team")
	newTeam.TeamColor = BrickColor.new(i + 20)
	newTeam.Name = v
	newTeam.Parent = game.Teams
	
	local newButton = game.ReplicatedStorage.Frame:Clone()
	newButton.TextButton.Text = v
	newButton.Parent = game.StarterGui.ScreenGui.Frame
end

as you can see i create a button for every job in my table, how would i go about making it so that whenever i pressed the button that was created, it can do something related to the button (eg. put me on a team same as it's index)

1 comment

0

You can connect the button you make inside the loop as well

1 answer

It's possible to put the code inside the for loop, too:

for i,v in pairs(teams) do
	v.MouseButton1Down:Connect(function()
		--code
	end)
end

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