Changing a value through a saved variable value?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Like the title says, I want to somehow change a saved value variable’s instance to a specific value.**

  2. What is the issue? Include screenshots / videos if possible!
    Not really an issue.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have searched and searched and searched but yeah, I dont think anyone would and feel the need to do what I am trying to do right now.

Example of what I mean:

local Value = workspace.NumberValue.Value
Value = 8

The code above would not change the NumberValue’s value given the fact its simply an already saved variable.

local Value = workspace.NumberValue
Value.Value = 8

The code above would thought.

Is there anyway the value can be changed through the use of an already given value like the first script? Also please refrain from asking questions related to why I need to do this, please.

1 Like

The reason it does this is because variables are reassigned when you do that. Example:

local variable = "apple"
variable = "orange" -- variable is now orange as it has been reassigned

It would reassign the variable like that if you don’t say variable.Value.
I hope I explained this well.
Adding a .Value to your code may be annoying, but unfortunately I think it’s unavoidable while working with value instances.

Pretty sure this is impossible unless you do a for loop checking every value object in the game, which would have issues with duplicate values.

Maybe a function?

local function set(inst, val)
    inst.Value = val
end

local num = Instance.new("NumberValue")
set(num, 5)

Your example wouldn’t work as a = b.Value returns a copy of value from B into A, it’s passed-by-value instead of passed-by-reference

Here’s a post just like yours with a descriptive solution that may be the one you are also looking for.

print(numberValue) --> 8
print(game.Workspace.NumberValue.Value) --> 0

numberValue would still be a by-value copy disconnected of NumberValue

1 Like

Yes, I misinterpreted his question and was too quick to jump on the solution.

1 Like