Loop through table in order

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.

You can firstly sort the table based on price, then numerically loop through it and check if they can afford it, if they can, deduct the amount from their money and give it to them, otherwise break the loop, it’ll keep doing this until either they buy everything, or don’t have enough to buy anymore.

Oh okay thanks for telling me.

I just tested the print out and the name prints nil and the price 0

Could you send the code? I’ll have a look.

Okay I fixed it up. I’m gonna make the system so it buys all it can buy.

@ZethusImam Is correct, you need to loop through the loop and check if they have the right amount then buy all items they can then exit the loop.

for _, v in ipairs(shopData.Energies) do -- maintain order when iterating through a table numerically indexed
    		local name, price = v.ItemName, v.Price
			print(name, price)
			if price <= player.leaderstats.Coins.Value then
				client.Data.Coins = client.Data.Coins - price
			else
				print(name,price)
				break
			end
		end

Did I do this right then?

Try running your code, does it work as planned?