Ad
Log in to vote
0

Why is NumberValue Addition not working?

Asked by 1 month ago

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

Log in to vote
0
Answered by
Ziffixture 6785 Moderation Voter Community Moderator
1 month ago
Edited 1 month ago

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)
0
Thank you! You're a life-saver! WhatsSamYT 9 — 1mo
Ad

Answer this question