Sorry in advance for broken English and bad coding, I just started getting into it T-T
-
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
-
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)
-
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