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

How do I get a table to be sorted the way it is as the code says?

Hello, I'm making a shop system that places itself onto a gui but its not listed as it should be seen on the "ShopList" table. For example it prints out Shotgun, Pistol and then Raygun where it should actually say Pistol, Shotgun and then Raygun. I don't know any solutions..

local ShopList = {
	['Pistol'] = {Image = "rbxassestid://00000"},
	['Shotgun'] = { Image = "rbxassestid://00000"},
	['Raygun'] = {Image = "rbxassestid://00000"},

}

for Table,Info in pairs(ShopList) do
	print(Table)
	print('Loaded '.. Table..' Shop item')
end

2 comments

0

There seems to be a print function, what's outputted?

0

It shows as this "Shotgun, Raygun, Pistol"

1 answer

✓ Accepted answer

Dictionaries cannot be organized in a certain order. If you want it to do that you could either create a table in which you print it in the order you'd like:

local shopList = {
	['g1'] = '12344'
	['g2'] = '44321'
}

local order = {'g1', 'g2'}
for i,v in pairs(order) do
	print(shopList[v])
end

or you can not use table.sort, except you'd have to format your table differently.

local shopList = {
	{'g1', 'asset/1234', 1}
	{'g2', 'asset/4321', 2}
}

table.sort(shopList, function(a,b)
	return a[3] > b[3]
end)

for i,v in pairs(shopList) do
	local itemName = v[1]
	local assetId = v[2]
	local order = v[3]
end

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