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

Why is NumberValue Addition not working?

I'm trying to add numbers to a NumberValue for my gameshow but it won't work. Can someone check my code?

local Value = workspace.Blue.Sign.Value.Value
script.Parent.ClickDetector.MouseClick:Connect( function()
	Value + 100
end)

1 answer

✓ Accepted answer

You cannot make pointers to attributes in Lua. Your code is actually storing the state of Value at runtime, rather than making a reference to the property, "Value".

You also only performed an addition operation on Value, which would have evaluated to n + 100 on that line. You need to actually reassign Value as the addition result:

Value = (Value + 100)

####Note

I highly recommend you do not name an Instance after an attribute affiliated to it, E.g Value.Value. I'd rename it to NumberValue, or give it a name that gives context to its purpose.


If you wish to change the property, you'll have to manually index it every time:

local NumberValue = workspace.Blue.Sign.Value


script.Parent.ClickDetector.MouseClick:Connect(function()
	NumberValue.Value += 100 --// LuaU supports shorthand operands.
end)

1 comment

0

Thank you! You're a life-saver!

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