local dec = game.Workspace.Stages:GetDescendants() for a,b in pairs(dec) do local Stage_Value = game.Workspace.Stage_Value.Value local stage_convert = game.Workspace.Stage_Value.Value + 1 local convert = tostring(stage_convert) b.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then if b.Name == convert then Stage_Value = tonumber(b.Name) end end end) end
It works, but the "number value" never updates, what could I be doing wrong ?
Common mistake. You are wrongly assuming Stage_Value
is a reference to the actual Value
property. It is not. When you re-assign your variable the Value
property will not change, only the variable, and Stage_Value
will not change when the Value
changes either.
References refer
(or "point") to certain data.
For example, functions are passed by reference.
```lua local fn1, fn2, fn1ref;
fn1 = function() for i = 1, 12 do print(i^2); end end
fn2 = function() for i = 1, 12 do print(i^2); end end
fn1ref = fn1; ```
fn1
and fn1ref
have the same exact function in memory. fn2
is different from the two even if it has the same exact code. Same indentation, line count, spacing, all that (not that they mean anything too important to the syntax itself but to show the similarities).
```lua print(fn1 == fn1ref); --> true print(rawequal(fn1, fn1ref)); --> true --// ^ just to show you there is no __eq metamethod
print(fn1 == fn2); --> false print(function() end == function() end); --> false ```
Apart from references
there is also values
. For example, numbers
are passed by value.
lua
local num = 5;
local num2 = num;
num = num * 4;
print(num, num2); --> 20 5
num2
gets a "copy" of num
but is not referring to num
. Thus, there is different memory and when num
is changed num2
does not change.
Make Stage_Value
refer to the IntValue/NumberValue
object itself and when you need to write to a property use Stage_Value.Value = someNumber
and when you need to read
something from the object use Stage_Value.Value