this is something Ive been having doubts about, How can stop a function from running after it runs x number of times?
(if I already have the function written and want to call it)
sorry for not being specific
this is something Ive been having doubts about, How can stop a function from running after it runs x number of times?
(if I already have the function written and want to call it)
sorry for not being specific
You could have a variable that counts how many times a loop has ran, and if it exceeds stop the loop.
ex:
local counter = 0
while counter < 5 do
print("I ran "..counter.." times!")
counter = counter + 1
end
Edit: below solution and this one will work
Here is a way to do this:
local counter = 10
for i = counter, 0, -1 do
end
This will run 10 times (or however many times you want)
just do a for loop:
for x = 1,number, +1 do
end
This is an easier way to do it because you don’t need and other loops or things, just set number to how many times you want to repeat and you are done!
if you still don’t understand then use repeat loops:
local x = 0
repeat
wait()
x = x + 1
until x = number
You might be needing a for loop. For loops loop some code for a certain number of times, then it just continues through the script. Here’s an example.
for i = 1, 3 do
print(i)
end
The i we see here is a variable for the loop. It is the number that the loop is currently on, so if the code loops 5 times, and you print i for every loop, the output will show 1, 2, 3, 4, and 5.
The first number is the number that the i variable starts on. It doesn’t have to be 1. It can be 2, 10, and -5. The second number is the number that i stops on. If you start it with 2, and end it with 3, it will loop two times, since it starts at two, then goes up by one to three.
for i = 3, 5 do --loops 3 times
for i = 5, 15 do --loops 10 times
for i = -3, 3 do --loops 6 times.
For loops have an optional third number that is what i increases by every loop. It’s one by default, but you can change it if you want to.
for i = 1, 5, 2 do --loops 3 times
for i = 1, 4 do --loops 4 times (no increment number is provided, so it goes up by one every loop)
for i = 1, 3, 3 do --loops 2 times (i would go up to 4, but the loop will stop because i is greater than the end number)
use a variable and set it to the max number ( or how many times you want to repeat )
then place a for / repeat loop after it and just don’t call the function after
local numberOfTimes = 1
for i, v in range(1, numberOfTimes) do
--what you want to run
end
sorry if the post is bad quality this is my first post
For i = 1, 100 do
Print("this will run 100 times")
Print(i.."/100") -- It will print something like 50/100
Wait() -- Without wait it will become almost instant, if your running a multi-thread script then have it as task.wait()
End
I is a variable defined inside of the loop it. If you try printing it outside of the loop then it will not work. You can also change I to whatever you want.
The number 1 is the amount to add between each loop if I were to change it to 2 then it will add 2 each time it runs until it becomes 100.
100 is the max number until you want it to run to, you can change this to anything expect decimals and maybe negative numbers (I’m not sure, it might still work with negative numbers).
This might be late, but how do I make it that the counter is reset back to 0 the next time the script is ran?
run the script again ig i dont think the counter will save unless i is a variable or something else