Attempt to perform arithmetic on field 'Multiplier' (a nil value)?

I have a system in place which allows the player to increase the amount of coins he gets.
However, when increasing the amount of coins the person can get an error occurs,

‘Attempt to perform arithmetic on field “Multiplier” (a nil value)’
If anyone could help me as to what this means or how to fix it, i’d appreciate it.
Here’s the code:


function module.ChangeCoinMultiplier(plr, key, value)
		if db == false then
			db = true
			local UserData = datastore2(MainKey, plr):Get(SetDataTable())
			local values = datastore2("Values", plr)
			UserData.Values.Multiplier = value
			UserData.Values.Points = UserData.Values.Points - 1
		if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId, 7031507) then
			local multiplier = 1 + (0.5 * UserData.Values.Rebirths)
			UserData.Values.Multiplier = (UserData.Values.Multiplier / (multiplier)) / 2
			UserData.Values.Multiplier = (UserData.Values.Multiplier * multiplier) * 2
		else
			local multiplier = 1 + (0.5 * UserData.Values.Rebirths)
			UserData.Values.Multiplier = UserData.Values.Multiplier / (multiplier)
			UserData.Values.Multiplier = UserData.Values.Multiplier * multiplier
		end
		values:Set(UserData.Values)
		wait(1)
		db = false
		end
	end

This means that

UserData.Values.Multiplier

is nil - has no value assigned to it, therefore you can not perform arithmetical operations on it for example here:

UserData.Values.Multiplier = (UserData.Values.Multiplier / (multiplier)) / 2

Sorry for the edit, the problem is in another place so I removed a part of what I have written.

Oh okay, i see now. That still wouldn’t explain why it has nothing assigned to it however, because UserData.Values.Multiplier has a value.

It’s defined in

local UserData = datstore2(MainKey, plr):Get(SetDataTable())

SetDataTable being a returned value

local function SetDataTable()
	local DefaultData = {
		Values = {
		["Coins"] = 0,
		["Gems"] = 0,
		["Experience"] = 0,
		["Levels"] = 1,
		["Points"] = 0,
		["Defense"] = 1,
		["Speed"] = 16,
		["Multiplier"] = 1,
		["Rebirths"] = 0,
		["Maps"] = 1,
		["Joined"] = 0,
		},
		
		Achievements = {
		["Alpha"] = 0,
		["Level_10"] = 0,
		["Level_25"] = 0,
		["Level_50"] = 0,
		["Level_100"] = 0,
		}
		}
	return DefaultData
end

I just saw your edit now, the problem might occur in the table above?

The problem is with the value passed to the function

function module.ChangeCoinMultiplier(plr, key, value) -- value is nil

aghhh! you’re right i was so oblivious to that, i will try find the problem. Thank you!

–Edit: Found the problem, Thank you again!

1 Like

Good to hear that, good luck with the game!

1 Like