Loop through table in order

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

Once you have table of prices sorted, check if the user has enough money for each one and if they do, then buy them all. Then subtract the amount.

@jumbopushpop112 Could you provide an example?

I don’t want to just give out code, but I’m going to give you some hints. You need to check if the user has the amount of cash needed

I’m thinking I could use a for loop and get the amount of each item they can afford and then buy each of them?

Yes, you can use that. Check each item in the table and see if the user has the exact amount.

And if they do, the buy the item.

Have you tried a numeric for-loop?

Nope I am gonna try it right now

Uh huh but I’m kinda confused about buying it in order though.

I don’t understand? What do you mean by buying it in order?

Like buying from least to greatest based on price on how much they want to buy it from.

That is if they press buy all then it buys all the tools tehy can afford.