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