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

How to add a value to a value? I can't.

Asked by 5 years ago

Basically what I want to do is to use values to do some math. Something like Prime Factorization to find out the index notations of a number. Here is my script.

Number = 200
Divide = 2

for i = 1,5 do
    wait(0.5)
local result = print(Number/Divide)
local Divide = Divide + 1
end

The output is just 100 and multiple of it. I do know that line 7 is the cause as it doesn't add up. Any way to add a number to a value? Thanks in advance.

1
Change line 1 and 2 into local variables and then don't use local in line 7. Rheines 661 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

What happens in your code is for every iteration in the loop, it creates a local copy of the Divide variable, then adds 1 to that copy, instead of the Divide variable you defined first outside of the loop.

The solution is to simply remove the local keyword on line 7.

Really though, the error here has to do with scopes and stuff. When you define a variable as local, what happens is it's only accessible inside the scope or "block" of code that it's running in. This could get technical really quick, so I'd suggest you read this instead.

Hope that helps!

1
Thank you!???????? FadedJayden_Dev 118 — 5y
0
You're welcome! Aniline_Purple 266 — 5y
Ad

Answer this question