Create VGUI elements in a for loop?

How do you create them in a for loop? I made my vgui menu as normal but there is a certain part that needs to be spaced and replicated, but my way wasn’t working!

Here’s the code keep in mind most of the information is being drag from a table so don’t worry about that part:
[LUA]local function NPCShopMenu( sg )

local ply = LocalPlayer()
local clk = clerks[sg]

chat.AddText( clk.ColorScheme.mid, clk.name .. ": ",Color(255,255,255), "Welcome to my shop, how can I help you?" )

d = vgui.Create( "DFrame" )
d:SetSize( 640, 432 )
d:Center()
d:SetDraggable( false )
d:ShowCloseButton( false )
d:SetSizable( false )
d:SetTitle( " " )
d:SetScreenLock( true )
d:SetDeleteOnClose( true )
d.Paint = function()
	draw.RoundedBoxEx( 6, 0, 0, 640, 432, clk.ColorScheme.light, true, true, true, true )
end
d:MakePopup( )

local bar = vgui.Create( "DShape", d )
bar:SetType( "Rect" )
bar:SetPos( 0, 0 ) 
bar:SetColor( clk.ColorScheme.dark )
bar:SetSize( d:GetWide() , 54 ) 

--rgb(255,153,153)
local lname = vgui.Create( "DLabel", bar )
lname:SetSize( 640, 54 )
lname:SetPos( 10, -10 )
lname:SetFont( "namePlateMenu" )
lname:SetTextColor( clk.ColorScheme.mid )
lname:SetText( clk.name )

local ldesc = vgui.Create( "DLabel", bar )
ldesc:SetSize( 640, 54 )
ldesc:SetPos( 12, 15 )
ldesc:SetFont( "namePlateTiny" )
ldesc:SetTextColor( clk.ColorScheme.mid )
ldesc:SetText( clk.desc )

local but = vgui.Create( "DImageButton", d )
but:SetPos( 590, 10 )
but:SetSize( 32, 32 )
but:SetImage( "gui/close_32.vtf" )
but.DoClick = function()
	d:Close()
end

local dcp = vgui.Create( "DScrollPanel", d )
dcp:SetSize( 340, 378 )
dcp:SetPos( 0, 54 )

local sbar = dcp:GetVBar()
function sbar:Paint( w, h )
	draw.RoundedBox( 0, 0, 0, w, h, Color( 0, 0, 0, 150 ) )
end
function sbar.btnUp:Paint( w, h )
	draw.RoundedBox( 0, 0, 0, w, h, clk.ColorScheme.dark )
end
function sbar.btnDown:Paint( w, h )
	draw.RoundedBox( 0, 0, 0, w, h, clk.ColorScheme.dark )
end
function sbar.btnGrip:Paint( w, h )
	draw.RoundedBox( 0, 0, 0, w, h, clk.ColorScheme.dark )
end

local dist = 80
local current = 5
local multi = 1
local spacing = ( dist + current )*multi

for k,v in pairs( clk.item ) do
	local j = getItems( v )
	
	local df = vgui.Create( "DPanel", dcp )
	df:SetSize( 320, 80 )
	df:SetPos( 5, spacing )
	df.Paint = function()
		draw.RoundedBox( 4, 0, 0, 320, 80, clk.ColorScheme.dark )
	end
	
	local i = vgui.Create( "DModelPanel", df )
	i:SetPos( 0, 0 )
	i:SetSize( 80, 80 )
	i:SetModel( j.model )
	i:SetAnimSpeed( 0.1 )
	i:SetAnimated( true )
	i:SetAmbientLight( Color( 50, 50, 50 ) )
	i:SetDirectionalLight( BOX_TOP, Color( 255, 255, 255 ) )
	i:SetCamPos( Vector( j.buttonDist - 18, j.buttonDist - 18, j.buttonDist - 18 ) )
	i:SetLookAt( Vector( 0, 0, 0 ) )
	i.Entity:SetSkin( j.skin or 0 )
	i:SetFOV( 20 )
	
	local In = vgui.Create( "DLabel", df )
	In:SetSize( 320, 80 )
	In:SetPos( 80, 14 )
	In:SetFont( "namePlateItem" )
	In:SetTextColor( clk.ColorScheme.mid )
	In:SetText( j.name )
	
	multi = multi + 1
end

end[/LUA]

[editline]5th May 2015[/editline]

anyone? :stuck_out_tongue:

Am I missing any info your looking for?

Incrementing ‘multi’ isn’t going to change the value of ‘spacing’.

opps I left out part of this ignore that, it’s part of my non working method I just want to know how to do this not what I did wrong cause I already know that :smiley:

That’s basically what I was doing it

You mean simply to position your elements in a loop?
[lua]
local y = 4
for k, v in pairs(tbl) do
local pnl = vgui.Create(“DPanel”)
pnl:SetSize(64, 64)
pnl:SetPos(4, y)
y = y + pnl:GetTall() + 4
end
[/lua]

I did something like that and it only created one element

Did it create only one element, or did it create a bunch of elements on top of each other? Because that’s what the code you gave in the OP is gonna do, assuming clk.item is a list with more than one item in it – you’re setting your new panel’s position to (5, spacing) but not changing the value of ‘spacing’.

Unless I did my math wrong but I had
[LUA]local current = 85
local multi = 1
local spacing = current*multi
for k, v in pairs(tbl)
(Panel stuff)
Pnl:SetPos(5, spacing)
local multi = multi + 1
end

[/LUA]

Ps current is the height of the element

Spacing never gets incremented in this loop. It just gets set once and left. Add a line to the loop that increments spacing properly and that should fix it.

Oh lol thanks :slight_smile: