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

Trying to list every player in the table? Any advice?

Jeah so I got a Frame and I want to display every Player who is in game with a button. So if there are 2 players in the game it will display 2 buttons with each name of the player. Everything works but they stay in the same position somehow. I want them to go down ( if you know what i mean). Like, they are in the same position but I want one to be in 0,0,0,0 and the next one to be in 0,0,0,5 and the next one in 0,0,0,10 . . .

	
	local gui = script.parent.Parent
	local open = gui:WaitForChild("Open")
	local frame = gui:WaitForChild("Frame")
	local holster = frame:WaitForChild("Holster")
	local spectate = frame:WaitForChild("Spectate")
	local sponsor = frame:WaitForChild("Sponsor")
	local shop = frame:WaitForChild("Shop")
	local premium = frame:WaitForChild("Premium")
	local template = holster:WaitForChild("template")
	
	local playersintable = {}
	
	local fieldSize = template.Size
	local fieldBorder = {x = 0, y = 5}
	
	spectate.MouseButton1Click:connect(function()
    	print("clicked")
		createplayers()
	end)
	
	function createplayers()
		for i, players in pairs(game.Players:GetPlayers()) do
			table.insert(playersintable,players.Name)
		end
		
		
		local toShow = #playersintable 
		local totalX = (toShow*fieldSize.X.Offset)+((toShow+1)*fieldBorder.x)
		local totalY = fieldSize.Y.Offset + (2*fieldBorder.y)

	for i = 1, #playersintable do
		print(#playersintable)
		local value = playersintable[i]		
		local clone = template:Clone()
		clone.Parent = holster
		clone.Text = playersintable[i]
		clone.Name = playersintable[i]
		clone.Visible = true
		clone.Position = UDim2.new(0, (i-1)*(fieldSize.X.Offset)+(fieldBorder.x*i), 0, fieldBorder.y)
		end	
	template:Destroy()
	end

1 comment

0

there is something called a UIlistLayout that should do exactly what you want, so you just have to loop over the player table (game.Players:GetPlayers()) and make a button for each of them (make sure to delete old ones though when you refresh the list)

1 answer

✓ Accepted answer

You could position it based on the iteration of a for loop. Consider the following:

local tab = {
	"foo";
	"bar";
	"hello";
}
for i,v in pairs(tab) do
	print(i,v)
end

Output:

1	foo
2	bar
3	hello

As you can see, i can be used as your offset factor. Now, consider this:

for i,v in pairs(game.Players:GetPlayers()) do
	-- create your frame
	frame.Position = UDim2.new(0, 0, 0, (i-1)*50)
end

This will create a frame for every player and position them 50 pixels apart. i-1 is used because arrays in Lua start at 1, and brackets are used because of order of operations; we need to make sure i is zero-based before we multiply by our amount.

Hope I helped!

2 comments

0

welp, you know, UIlistlayout exists? It's a bit better but then again, your method should work fine too

0

That works :)) Thanks! Helped me out alot.

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