Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

can someone explain why this isnt working in my script? i have no clue

Asked by 2 years ago

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

0
*also on a alt acc dont mind anything about this Death_rename 7 — 2y
0
Please, use a code block. It's literally like reading an encrypted note without deciphering it. NotThatFamouss 605 — 2y

1 answer

Log in to vote
0
Answered by
RAFA1608 543 Moderation Voter
2 years ago
Edited 2 years ago

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.

Ad

Answer this question