local X = 0 local Y = 0 local Z = 30
while wait() do wait(1) print(Y) print(Z) if Y == 30 then break else local Y = Y + 1 local Z = Z - 1 end end
Trying to get it to add 1 to y until it hits 30 but it constantly stays at 0 and doesnt go up i have no clue what im doing wrong, im way to tried for this
Hello! It seems your problem is with whether a variable is a global one or a local one.
A local variable can only be seen in the same "scope" (you can think of them as "towers"):
local x = 0 do --scope (tower) local x = 2 --changes variable within the tower end print(x) --0 because below the tower, no one can see anything changing
However, global variables are different. It is like, sending someone to change the variable below.
local x = 0 do --scope (tower) x = 2 --sends someone to change the value x below end print(x) --2 because the tower sent someone to change it
There is a way more convenient way of doing the loop you've done in your code, however, they are practically the same? which is:
local start = 0 local finish = 30 local increment = 1 for index = start, finish, increment do wait(0.5) print(index) --number between 0 and 30 end
Anyway, if you have any questions, fell free to ask me them.