How to make a loop run certain number of times based on how much a number value is

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I am making a inventory like system in a round based game. I made a number value that is named what the object is and then the value is how much of it that player has. Then they will spawn in and it will clone however many times the value is.

  2. What is the issue? I have already got this working because I would subtract the value every time it plays and make it not play if it is at zero. But once I make a saving system, if you are in the round it wont save the amount of potions you have if you are in the round, because I made it zero cloning the potions.

  3. **What solutions have you tried so far?**I’ve looked it up many times and tried to change my code, but it didn’t work. I just don’t know how I would word it.

(This is my first time making a post and it is probably very unclear, so please ask questions if you dont understand.)

1 Like

Can you show images or some of your code so I can better understand?

I don’t understand, send some code and elaborate on what you need help on

I don’t completely understand your issue. Can you post your code in a reply to let me look at it and ill see what’s wrong with it? Do you know how to add code?

Sorry, I was gone for a little, im not sure how to add code onto here. Should I just send the whole script?
i figured it out

while wait(0.5) do
	for i,plr in pairs(game.Players:GetChildren()) do
		 ammountofjump = plr.Potions.JumpPotions
		if plr.team == game.Teams.Playing then
			print("Playing")
			if ammountofjump.Value >= 1 then
				wait(1)
				local jumpclone = game.ServerStorage.Potions.JumpPotion:Clone()
				jumpclone.Parent = plr.Backpack
				ammountofjump.Value = ammountofjump.Value - 1
				end
end
end

This does what I want but it makes the value go down, is there a way to make it run through the loop as many times as the value is without changing it?

Im gonna just post this again.

for i = 1, number do
	--Do code.
end

Use a numeric for loop.

Try this:

while true do
	task.wait(.5)
	for _,plr in pairs(game.Players:GetPlayers()) do
		ammountofjump = plr.Potions.JumpPotions
		if plr.team == game.Teams.Playing then
			print("Playing")
			for i = 1, #ammountofjump.Value do
				local jumpclone = game.ServerStorage.Potions.JumpPotion:Clone()
				jumpclone.Parent = plr.Backpack
			end
		end
	end
end