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

The same thing, different results?

Asked by 2 years ago

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?

0
First script is changing the value variable itself, whilst the second script gets the value and adds it to that value. JesseSong 3916 — 2y
0
Also, the first and second script is not the same thing. JesseSong 3916 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

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.
Ad

Answer this question