Buttons cloning to specific items of an array

  1. What do you want to achieve? Once a player loads in the game and clicks a button, I would like to create buttons with the text specific to the names in the table so that I don’t have to input them manually every time.

  2. What is the issue? Items don’t clone and the local script runs twice.

  3. What solutions have you tried so far? Here is my code:

local namesTable = {
	"Mario",
	"Deshawn",
	"Dude",
	"Max"
}

local sampleButton = script.Parent.FirstNamesFrame.Sample
local currentPosition = 0

local function createNamesButtons() 
	for index, name in pairs(namesTable) do
		currentPosition = 0 + 0.065

		local clone = sampleButton:Clone()
		clone.Position = UDim2.new(0.5, 0, currentPosition, 0)
		clone.Parent = sampleButton.Parent
		clone.Text = name
		
		print(name)
	end	
	
	print('Finished function')
end

createNamesButtons()

I know that storing the position in a local variable is bad practice and not efficient at all but I don’t seem to find any other way to do it.

Replace

currentPosition = 0 + 0.065

with

currentPosition += 0.065

Then it should work


The issue is that you were always giving them the same position (0 + 0.065) instead of a changing one (currentPosition + 0.065)


Side note: As you are not using a dictionary, pairs should be replaced with ipairs

1 Like

Oh right, what was I thinking. It took me ages to figure it out, thank you a lot for opening my eyes and helping me :slight_smile: