78: attempt to perform arithmetic on local 'Value' (a nil value)

hi, m pro_developer213 and i wanted to make a module script that can create a leaderstats that uses my own dataStore sistem but it get always this error:

78: attempt to perform arithmetic on local ‘Value’ (a nil value)

edit:

my script:


function K:DetectName(item,name,plr)
	local P = plr:WaitForChild('leaderstats')
	local Item = P:FindFirstChild(name)
	if Item then
		if item == "Value" then
			return item.Value
		elseif item == "Item" then
			return item
		end
	end
end

function K:Increment(valueName, value,plr)
	local Value = K:DetectName("Value",valueName,plr)
	Value = Value + value
end

The error you are seeing is because Value is nil. Based on your code it looks like that is because Item is nil which is returned by FindFirstChild. Check to make sure the instance exists.

2 Likes

hi, yes i have the Instance in the leaderboard, what is the poblem?

Maybe the instance has not loaded on the client yet. Try using WaitForChild somewhere and waiting for the instance to be added.

@Hexcede’s answer suffices but it’s worth mentioning that when updating a property, you must do this:

Instance.Property = newValue

Not this:

local value = Instance.Property -- Doing this gives value the value, not a reference to anything
value = newValue -- Changes only the variable's value, not the actual property

Hence in your script:

function K:Increment(valueName, value,plr)
    local Object = K:DetectName("Item",valueName,plr)
    if not Object then return false, "Object not found" end
	local Value = K:DetectName("Value",valueName,plr)
	Value = Value + value
    Object.Value = Value
end

This might be a OOP problem( or partly) , Make sure that you are calling your function right from your module, i believe you might be something like this:

(as an example)

local module = {}

function module:Test(value)
	print(value) ---this would print nil
end
	
return module

local module= require(game.ReplicatedStorage.ModuleScript)
module.Test('V')

To fix this is this is the problem consider using self

what self does? i heard about it at HDAdmin

Essentially self, in OOP is a hidden parameter that gets passed when using : in a function, it’s the first value that is passed from where you are calling the function( sorry if this isn’t a great explanation),

Example:

function module:Test(value) --- so instead of using value we should use self
print(self) ---10
print (value) --4 
end
local module= require(game.ReplicatedStorage.ModuleScript)
module.Test(10,4)

Better explanation:
https://www.lua.org/pil/16.html

Resources:

2 Likes

thanks but if is a thng like:

functon K:HI(Idk,Idk2)

end

self will be Idk?

Try using this:

function K:DetectName(item,name,plr)
	local P = plr:WaitForChild('leaderstats')
	local Item = P:FindFirstChild(name)
	if Item then
		if item == "Value" then
			return Item.Value -- You should use capital letter because you were calling called string
		elseif item == "Item" then
			return Item
		end
	end
end

The problem is that you cannot add arthmetic singns to string so try changing that. :herb:

1 Like

self, would not be Idk, it’s the first value passed, infront of idk and idk2( if that makes sense)

if this is an oop problem then changing the function should be one of the ways you can also fix this problem(without using self):

function K.DetectName(item,name,plr)

Wether this is problem with the specific code you provided i recommended that you do look more into OOP

Edit: also the value thing could be also (if not the) very well the cause of your problem

2 Likes