Creating buttons for every instance in an array

Hey scripters who know what they’re doing (me not being one of them!) - I’m trying to create a system that adds a new button into a ScrollingFrame for each instance in an array. I have a folder with all of the instances inside of my games Lighting folder, and I load them into my script using :GetChildren. For some reason, it doesn’t seem to do anything when I spawn in.

My current script looks like this:

local serveitems = game.Lighting.ServeItems:GetChildren()
local scrollframe = script.Parent
local buttontoclone = scrollframe.placeholder
local scripttoclone = scrollframe.setvaluescript

for i = 1, #serveitems do
	if i == 1 then
		local currentbutton = serveitems[i]
		local newbutton = buttontoclone:Clone()
		local newscript = scripttoclone:Clone()
		newscript.Parent = newbutton
		newbutton.Name = currentbutton.Name
		newbutton.Text = currentbutton.Name
		newbutton.Visible = true
	else
		local currentbutton = serveitems[i]
		local lastbutton = serveitems[i-1]
		local newbutton = buttontoclone:Clone()
		local newscript = scripttoclone:Clone()
		newscript.Parent = newbutton
		newbutton.Name = currentbutton.Name
		newbutton.Text = currentbutton.Name
		newbutton.Visible = true
		local actuallastbutton = scrollframe:WaitForChild(lastbutton.Name)
		newbutton.Position.Y = actuallastbutton.Position.Y + {0.044, 0}
	end
end

A screenshot of the explorer tab just to help:
image

I’ve given a few other posts a read on the DevForum, but couldn’t find anything to help me with this particular issue.

No errors print out in the Output.

I don’t know if I’ve just made a rookie error here, or if this is something that just isn’t possible, but any help you can give me would be much appreciated!

Thanks,
Kent :smile:

1 Like

Things I’ve noticed:

  • You didn’t parent the cloned newbutton. (Ex. newbutton.Parent = scrollframe)
  • Since it’s a ScrollingFrame object, I recommend stopping using this new button.Position.Y and use the UIListLayout object to do the same thing but it’s easier to understand and can be modified.
  • If you did the second bullet, then adjust your ScrollingFrame’s properties such as these:
    image
  • AutomaticCanvasSize for when the contents of the scrolling frame are too many then change the size of the canvas size.
  • CanvasSize is changed to 0 so that the scrollbar is not visible when there are no contents or the number of content is not overlapping inside the canvas.
  • ScrollingDirection are dependable on your UI if you want to scroll up-down or left-right.
1 Like

I find it better to do for _, i in pairs(serveitems) do.
With this, you can replace serveitems[i] with just i.

1 Like

If it’s an array (which in this case it is) you should use the ipairs iterator generator, not for the defined order in which it traverses but for the sake of indicating that the iterable is an array.

1 Like

Thanks so much for your comment - I’ve been out all day today so I will make sure to make these amends later on. I will mark as solution if these work!

Hey, thanks - I’ll definitely look into this. Is it more of a good programming practice though, than something I absolutely have to do for it to work?

Hey again, this worked! Thanks for your input :smiley:

It usually depends what you’re doing.
It’s best used for using GetChildren(), because the second parameter (i in this case) points to the instance.