DataStore2 problem: Attempt to perform arithmetic (add) on table and number

Hi. So recently, I’ve just started using DataStore2.

I’m using it for an inventory system. Problem: Whenever the item is purchased, I keep getting this error:

11:20:33.428 ServerScriptService.Leaderstats.DataStore2:191: attempt to perform arithmetic (add) on table and number - Server - DataStore2:191

Again, I’m new to using DataStore2 so any suggestions to resolve this issue are appreciated.

Server Script:

local dataStore2 = require(script.DataStore2)

local repStorage = game.ReplicatedStorage
local remotes = repStorage:WaitForChild("DataRemotes")
local buyItem = remotes:WaitForChild("Purchase")
local sendData = remotes:WaitForChild("SendData")

dataStore2.Combine("Key", "Money", "Inventory", "EquippedWeapons")

local starterMoney = 0
local defaultWeapons = {}
local equipped = ""

game.Players.PlayerAdded:Connect(function(player)
	
	local char = player.Character or player.CharacterAdded:Wait()
	
	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = player
	money.Value = 0
	
	local inventory = Instance.new("Folder")
	inventory.Name = "Inventory"
	inventory.Parent = player
	
	local equippedWeapon = Instance.new("StringValue")
	equippedWeapon.Name = "Primary"
	equippedWeapon.Parent = player
	equippedWeapon.Value =  ""
	
	local moneyDataStore = dataStore2("Money", player)
	local inventoryDataStore = dataStore2("Inventory", player)
	local equippedDataStore = dataStore2("EquippedWeapons", player)
	
	local function updateMoney(value)
		money.Value = value
	end
	
	local function updatePlayerInventory(tableToAdd)	
		for i, v in pairs(inventory:GetChildren()) do
			v:Destroy()
		end	
		
		for i, v in pairs(tableToAdd) do
			local item = Instance.new("BoolValue")
			item.Name = v
		end
	end
	
	local function updateEquippedWeapon(weapon)
		equippedWeapon.Value = weapon
	end
	
	updateMoney(moneyDataStore:Get(starterMoney))
	updatePlayerInventory(inventoryDataStore:Get(defaultWeapons))
	updateEquippedWeapon(equippedDataStore:Get(equipped))
	
	moneyDataStore:OnUpdate(updateMoney)
	inventoryDataStore:OnUpdate(updatePlayerInventory)
	equippedDataStore:OnUpdate(updateEquippedWeapon)
	
	sendData:FireClient(player)
	
end)

buyItem.OnServerInvoke = function(player, weaponName)
	
	local moneyDataStore = dataStore2("Money", player)
	local inventoryDataStore = dataStore2("Inventory", player)
	local equippedDataStore = dataStore2("EquippedWeapons", player)
	
	local weapon, isOwned
	
	local newTable = {}
	
	weapon = repStorage.WeaponModels:WaitForChild(weaponName)
	
	if player.Inventory:FindFirstChild(weaponName) then isOwned = true end
	
	local cost = weapon:GetAttribute("Cost")
	
	if not isOwned then
		if player.Money.Value >= cost then
			moneyDataStore:Increment(-cost)
			
			local item = Instance.new("BoolValue")
			item.Name = weapon.Name
			
			for _, v in pairs(player.Inventory:Getchildren()) do
				table.insert(newTable, v.Name)
			end
			
			inventoryDataStore:Set(newTable)
		end
	end
	
	print("Done!")
	
end

1 Like

Can you point to the line that is causing the error?

This line, moneyDataStore:Increment(-cost), is where the error comes from.

Assuming that this is the line giving you that error, it would have to be the attribute after reading the DataStore2 API, You seem to be using :Increment(-value) correctly, I would check what the attributed value is printing out, possibly the issue, or you are looking at the wrong line.

1 Like

Ah I see. Also…

…yes, I can confirm that the error is coming from that line; I set the attribute as a number value and when I printed moneyDataStore:Get(), it was actually a table so I figured that’s where the problem was coming from.