Reputation: 67
To clarify the question title, I have a for
loop that runs an amount of time depending on the number variable value that is above 0. So instead of creating a variable that the value is on a method returned value, I just put the method but same result, it still doesn't work.
I searched on Google but it was with while
loops and not for
loops (even if the title contains for loop or it is just a mistake because they didn't use #
before or anything like that). The code is very simple as you can see:
if getItemNumber() > 0 then
local numb = 0 --This will be the price after the loop
local loopNumb = getItemNumber()
for i=10, loopNumb, 10 do --I start with i=10 and it executes for each items and increments by 10 everytime so it will add the loopNumb number times 10 for the price because 1 item = 10 money. (sorry for being difficult to understand)
print(i) --I need to print to check if the loop executes, it does not.
numb += loopNumb * 10
end
print(numb)
--more code but not related to the problem and I need this loop for the code after.
end
If you need to see the getItemNumber()
function, look at this code:
function getItemNumber() : number
local currString = script.Parent.Parent.NumberOfItemsTxt.Text
local currNumb = tonumber(currString)
if currNumb ~= nil then
print("valid")
return currNumb
else
print("nil")
return 0
end
end
I tried replacing loopNumb
by the method directly but it still does not run. Did I miss something or is it a bug?
Note: the loop is not a nested loop and it is inside a click event on a gui element (TextButton) in a LocalScript
Upvotes: 0
Views: 397
Reputation: 7188
The numbers you pass to a for-loop instruct the loop where to start and where to end.
The first number is the starting point. The second is the ending point, and the third is how large of a step you take to get there.
For example :
for i=1, 10, 2 do
The variable i
will start at 1, and then every loop will add 2 until i
is greater or equal to 10. So : 1, 3, 5, 7, 9, done.
In your code, you've said that you are passing in values like 2 and 3 which end up being equivalent to :
for i=10, 2, 10 do
... start at 10, and in steps of 10 at a time, please loop until you are at 2. Since i
is already beyond the end limit, your loop exits immediately and does not run.
It's unclear from your code what you want to happen, but try reducing the numbers to just the number of times you want it to loop.
for i = 1, loopNumb, 1 do
Upvotes: 1