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