Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Attempt to index local 'Value'(a number value) error while using .Changed?

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)

1 answer

✓ Accepted answer

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

#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
-- 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.

##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) --> 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.

#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) -- this is not the player. This is the new value.
        if new % 5 == 0 then
            Gems.Value = Gems.Value + 10
        end
    end)
end)

1 comment

0

Such a well thought out and worded answer! Thank you this helps me tremendously.

This discussion is preserved as it was. New answers, comments, and votes are closed.