Line 7 is the problem.
Value.Changed:Connect(function(plr)
And line 4 is the culprit
local Value = Stage.Value
The problem here is the fact that you wrongly assume that the Value variable refers to the actual property of the Stage.
This example would not work as expected:
local int = workspace.IntValue.Value
int = 92
print(workspace.IntValue.Value)
Though this would:
local int = workspace.IntValue
int.Value = 50
print(int.Value)
int.Value = 92
print(int.Value)
#Reference vs Value
It is important that you know the difference between a reference and a value.
##Reference
In Lua, for example, functions are passed by reference.
local func1, func2
func1 = function()
print(1)
end
func2 = function()
print(1)
end
local func1reference = func1
print(func1 == func2)
print(func1 == func1reference)
print(function() end == function() end)
The func1 and func1reference variables both hold the same exact function. func2 is a different function from func1 even if it contains the same code. Remember, references.
##Value
Though data like booleans and strings are passed simply via value.
local _string = "hello"
local _string_value = _string
_string = "gzhsiikw"
print(_string_value)
local bool1 = false
local bool1_val = bool1
bool1 = true
print(bool1)
print(bool1_val)
Even though _string_value was set to _string, both variables have different memory and modifying one value does not change the other. The same happened for the boolean values, bool1_value never changed even after updating bool1.
#Final product
game.Players.PlayerAdded:Connect(function(plr)
local PlayerStats = plr:WaitForChild("leaderstats")
local Stage = PlayerStats:WaitForChild("Stage")
local Gems = PlayerStats:WaitForChild("Gems")
Stage.Changed:Connect(function(new)
if new % 5 == 0 then
Gems.Value = Gems.Value + 10
end
end)
end)
1 comment
Such a well thought out and worded answer! Thank you this helps me tremendously.