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

Stage system not updating "number Value" ?

Asked by 5 years ago
Edited by TheeDeathCaster 5 years ago
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 ?

0
What exactly are you trying to do? IDKBlox 349 — 5y
0
Your Touched event should be outside of the loop and the loop should be inside of the event. DeceptiveCaster 3761 — 5y
0
I put the code in a code block. TheeDeathCaster 2368 — 5y
0
line 4 and 5 are deprecated, they shouldn't be. User#25281 0 — 5y
View all comments (2 more)
0
No they are not? User#24403 69 — 5y
0
@habibibean They are not deprecated. Open your eyes. They're the value and adding 1 to the value. DeceptiveCaster 3761 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

You are using values, not references

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.


What are references

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 ```


What are values

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.


How to fix your problem

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

Ad

Answer this question