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

The same thing, different results?

Recently I was working with a script using values and encountered a problem. I noticed that values don't change when using variables. Here is what I mean:

local value = script.TEST.Value --test being an intvalue

value = value +1

I noticed this did not change the value. Value is still 0. However if I write what I think is the exact same thing:

script.TEST.Value = script.TEST.Value +1

Guess what? Value changed! It is now equal to 1.

I have looked online for a bit and not found anything regarding this, I am really confused why this is happening. Could anybody explain?

2 comments

0

First script is changing the value variable itself, whilst the second script gets the value and adds it to that value.

0

Also, the first and second script is not the same thing.

1 answer

✓ Accepted answer

When you do value = value + 1, your not changing the Value property of Test. Your actually assigning a new Value to the variable.

local Variable = script.TEST.Value 

Variable = Variable + 1 -- Same as: Variable = script.Parent.Value + 1
print(script.Parent.Value) -- prints 0, through this entire script, the value has never been changed.
print(Variable) -- prints 1, Variable's value has been changed on line 3.

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