Saving a previous variable

Hello, everyone!

I recently had a problem when using variables. I am writing a script to load and quickly reset a player’s data, and then return it to the previous value. However, I cannot seem to find a way to store the original value without setting it to 0.

Consider this script:

player.data.item.Value = x
    wait(0.01)
player.data.item.Value = 0 
    wait(0.01)
player.data.item.Value = x

This is what I am trying to achieve, however none of the solutions I have tried in studio have worked as they have overwritten the variable.

If I created a variable x to save the original number by connecting it to the item.Value, it would override at line 3 as it would receive the Changed signal and would remove the original value, making the data useless. Even if I created a variable in the actual function, it still would not save as it would (again) overwrite itself.

This question is incredibly basic, however I cannot find a solution that works after about 10 minutes of tinkering. How would I save a variable with a value that cannot be overwritten?

Thank you for reading this post.

Store the previous value first

local previous = player.data.item.Value
wait(0.01)
player.data.item.Value = 0
wait(0.01)
player.data.item.Value = previous
1 Like

As I said in my post, the previous variable you mentioned would get reset to 0 at line 3.

Have you even tried it? The variable doesn’t update with the value property.

Observe:

local int_value = Instance.new("IntValue")
int_value.Value = 50
local v = int_value.Value
print(v) -- 50
int_value.Value = 2345
print(v) -- 50

My apologies. Thank you for the help.