This code is giving me the error in the title, i've tried adjusting the code several ways but cant get around the error. Any ideas?
game.Players.PlayerAdded:connect(function(plr) local PlayerStats = plr:WaitForChild("leaderstats") local Stage = PlayerStats:WaitForChild("Stage") local Value = Stage.Value local Gems = PlayerStats:WaitForChild("Gems") Value.Changed:Connect(function(plr) if Value % 5 == 0 then Gems.Value = Gems.Value + 10 end end) end)
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:
-- Currently the IntValue holds a Value of 25. local int = workspace.IntValue.Value int = 92 -- Here it's just reassigning 'int'. print(workspace.IntValue.Value) --> 50, it does not change even after reassigning 'int'.
Though this would:
-- Starting value of IntValue.Value is 25 local int = workspace.IntValue int.Value = 50 print(int.Value) --> 50 int.Value = 92 print(int.Value) --> 92
It is important that you know the difference between a reference and a value.
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 -- func1 and func1reference are referencing the same function now, but func2 is different print(func1 == func2) --> false, not referencing the same function print(func1 == func1reference) --> true, func 1 reference references func1 print(function() end == function() end) -->false
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.
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) --> true print(bool1_val) --> false
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
.
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) -- this is not the player. This is the new value. if new % 5 == 0 then Gems.Value = Gems.Value + 10 end end) end)