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

Stage system not updating "number Value" ?

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 ?

6 comments

0

What exactly are you trying to do?

0

Your Touched event should be outside of the loop and the loop should be inside of the event.

0

I put the code in a code block.

0

line 4 and 5 are deprecated, they shouldn't be.

0

No they are not?

0

@habibibean They are not deprecated. Open your eyes. They're the value and adding 1 to the value.

1 answer

✓ Accepted answer

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.

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

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.

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

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