Loop through table in order

I’mt trying to make a buy all script like ninja legends basically by looping through a table inside of a module and subtract the amount of money they can use to buy the items.

I’ve tried finding a devforum post but I couldn’t. Here is the code:

Module Script


local Tools = {
	{Name = 'Test', Price = 5},
	{Name = 'Test2', Price = 3},
	{Name = 'Test3', Price = 2},
    {Name = 'Test4', Price = 7},
    {Name = 'Test5', Price = 6}
}

local ToolLibrary = {}

for index, object in pairs(Tools) do
	object.Index = index
	ToolLibrary[object.Name] = object
end

local DefaultTool = ToolLibrary.Test1 -- This is an example of the equipped tool the player has
local NextTool = Tools[DefaultTool.Index + 1] -- example of the locked tool the player has

return Tools
--------------------------------------------------------------------
Server:
local module = require(pathway.Module)

Remote.OnServerEvent:Connect(function(player)
    -- How would you go about buying all the things the player can afford in order.
    --Pathway to player's currency is player.leaderstats.Coins
end)

Does anyone know what to do for this?

Alrighty, so does the code work or not? Not sure what you are asking here.

Not exactly I’m trying to loop through the returned table from the module and get the lowest price basically I’ll edit it so I can have it like that and basically I wanna use the ToolLibrary to get the index from the table and then have the player on the remote event take away the money for all they can buy and update the default tool and next tool.

Well, for starters the module script isn’t going to return the table. Module is different then module. Change the return statement to return Module because that is your table name.

Is this one big script or 2 separate ones?

They are 2 seperate scripts above is the module and the bottom is the server

Alrighty, well change the name of the table to module. Return statement won’t return the table because of the names. And please change something other than module just for the purpose.

Oops I see what you mean sorry

@jumbopushpop112 are you still here?

Yes. I am to loop through a table, you need an iterator, and check the value at each part of the table.

local ToolLibrary = {}

for index, object in pairs(Tools) do
	object.Index = index
	ToolLibrary[object.Name] = object
end

local DefaultTool = ToolLibrary.Test1 -- This is an example of the equipped tool the player has
local NextTool = Tools[DefaultTool.Index + 1]

I can use this to get the things but like how would I do that to loop through returned table in order and start from least to highest and buy the items.

Mhmm, that is correct. Now you are most likely going to need to use the linear sort method to sort items in the table.

I used this in Java one time for a project with putting numbers from least to greatest.

I found out something for tables which is table.sort() how would that help me in this?

Well, the way I would do is first get each part of the table which you said that you did.

So, again tell me what you are trying to do.

Read this, someone had the same question.

I’mt trying to make a buy all script like ninja legends basically by looping through a table inside of a module and subtract the amount of money they can use to buy the items. I can use this to get the things but like how would I do that to loop through returned table in order and start from least to highest and buy the items.

I’m not sure I understand what you mean, but if you want to iterate through a table in order why don’t you use ipairs() instead of pairs()?

2 Likes