Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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 comment

1

Change line 1 and 2 into local variables and then don't use local in line 7.

1 answer

✓ Accepted answer

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!

2 comments

1

Thank you!????????

0

You're welcome!

This discussion is preserved as it was. New answers, comments, and votes are closed.