ServerScriptService.MyServer.MyInvServer:36: attempt to perform arithmetic (add) on number and string - Server - MyInvServer:36

Sorry in advance for broken English and bad coding, I just started getting into it T-T

  1. What do you want to achieve? I am trying to make a inventory with weight system and for that I need to track current weight

  2. What is the issue? I tried to add weight from the tool’s attribute to the current weight using +, but it didnt work. For unknown to me reason it counts my attribute as a string (I suppose)

  3. What solutions have you tried so far? I have bunch of solutions, yet I have no change.
    -I tried using tonumber(item.Weight) and it didn’t work.
    -I tried using tostring(inv.CurrentWeight)
    -I also tried making MaxWeight and CurrentWeight strings
    -I tried using Number Value (which I inserted into the tool) instead of attributes

The main point of interest in this code are these lines 20-43, 65-79. (If I am right)

--Services
local Players = game:GetService("Players")
local RepStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local StarterPack = game:GetService("StarterPack")
local HttpService = game:GetService("HttpService")

--Modules
local Janitor = require(RepStorage.Modules.Janitor)
local Signal = require(RepStorage.Modules.Signal)

--Module
local invServer = {}
invServer.AllInvs = {}
invServer.MaxSlots = 16



--Adding Items
function invServer.RegisterItem(player, tool)
	--Checks
	if tool.ClassName ~= "Tool" then return end
	--Getting Inventory
	local inv = invServer.AllInvs[player]
	--Adding an item
	if #inv.Inventory < invServer.MaxSlots and  inv.CurrentWeight < inv.MaxWeight then
		--Creating New Stack
		local item = {
			Name = tool.Name;
			Description = tool.ToolTip;
			Image = tool.TextureId;
			Weight = tool:GetAttribute("Weight");
			isPlaceable = tool:GetAttribute("isPlaceable");
			Item = {tool};
		} 
		inv.CurrentWeight = inv.CurrentWeight + item.Weight
		table.insert(inv.Inventory, item)
	else
		error("the player has too much ==-09")
	end
	--Updating Client
	Signal.FireClient(player, "Inventory:ItemsUpdated", invServer.GetInventoryJSON(player))
end

--Removing Item
function invServer.UnregisterItem(player, tool)
	if tool.ClassName ~= "Tool" then return end
	--Getting Inventory
	local inv = invServer.AllInvs[player]
	--Searching for the tool
	for i, itemData in pairs(inv.Inventory) do
		local found = table.find(itemData.Item, tool)
		if found then
			--Removing Tool
			table.remove(itemData.Item, found)
			
			inv.CurrentWeight -= found.Weight --Might be here
		end
	end
	--Updating Client
	Signal.FireClient(player, "Inventory:ItemsUpdated", invServer.GetInventoryJSON(player))
end

--Player Added
function invServer.PlayerAdded(player)
	--Waiting for starterpack
	for i, tool in pairs(StarterPack:GetChildren()) do
		while not player.Backpack:FindFirstChild(tool.Name) do
			task.wait()
		end
	end
	
	--Creating Inventory
	invServer.AllInvs[player] = {
		Inventory = {};
		Hotbar = {};
		CurrentWeight = 0;
		MaxWeight = 2;
	}
	
	--Registering Items
	for i, tool in pairs(player.Backpack:GetChildren()) do
		invServer.RegisterItem(player, tool)
	end
	
	--Connecting Events
	player.Backpack.ChildAdded:Connect(function(child)
		invServer.RegisterItem(player, child)
	end)
	
	player.Backpack.ChildRemoved:Connect(function(child)
		invServer.UnregisterItem(player, child)
	end)
	
	local function CharAdded(char)
		--Connecting Events
		char.ChildAdded:Connect(function(child)
			invServer.RegisterItem(player, child)
		end)
		char.ChildRemoved:Connect(function(child)
			invServer.UnregisterItem(player, child)
		end)
	end
	
	if player.Character then
		task.spawn(CharAdded, player.Character)
	end
	
	player.CharacterAdded:Connect(CharAdded)
	
end

--Getting Inventory Data
function invServer.GetInventoryJSON(player)
	--Waiting for the inventory
	while not invServer.AllInvs[player] do task.wait() end
	
	local inv = invServer.AllInvs[player]
	
	
	local adjustedInv = {
		Inventory = {};
		Hotbar = inv.Hotbar;
		CurrentWeight = 0;
		MaxWeight = 2;
	}
	
	for i, itemData in pairs(inv.Inventory) do
		table.insert(adjustedInv.Inventory, {
			Name = itemData.Name;
			Description = itemData.Description;
			Image = itemData.Image;
			Weight = itemData.Weight;
			isPlaceable = itemData.isPlaceable;
			Item = {itemData};
		})
	end
	
	--Returning
	return HttpService:JSONEncode(adjustedInv)
end

--Initializating
function invServer.Start()
	
	--Player Added
	for i, player in pairs(Players:GetPlayers()) do
		task.spawn(invServer.PlayerAdded, player)
	end
	Players.PlayerAdded:Connect(invServer.PlayerAdded)
	
	--Getting inventory from client
	Signal.ListenRemote("Inventory:GetInventoryJSON", invServer.GetInventoryJSON)
end

--Returning
return invServer
1 Like

Oh yeah, this is a module script, forgot to include

Are you sure tool’s attribute Weight is of type number or is a coerce-able string?

1 Like

Yeah I think so
image
It’s a number value

Number value as in when you created the attribute, you chose type “number”?

How about inv.CurrentWeight? Are you sure that’s a number?

Yea, like this
image

Inv.CurrentWeight should be number

That’s odd, try adding this right before you try adding inv.CurrentWeight and item.Weight and see what output it gives you

print(inv.CurrentWeight, type(inv.CurrentWeight), tonumber(inv.CurrentWeight), item.Weight, type(item.Weight), tonumber(item.Weight))

image

btw there are multipule tools in the starterpack

Are you able to get a screenshot of it when the error happens? I’m guessing one of the tools in the starterpack has a misconfigured attribute

1 Like

Okay, so the issue was that the tools in the workspace were old. Without the new attribute. Thanks for helping!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.