Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

[NOT WORKING?] Custom Inventory System

I have spent countless hours trying to get my inventory system to function properly, however i'm receiving an error on getInventory. Could i please receive some assistance?

Error:

21:48:19.641 - ReplicatedStorage._0xS0URC4X.Client.InventoryHandler:9: bad argument #1 to 'pairs' (table expected, got string)
21:48:19.642 - Stack Begin
21:48:19.642 - Script 'ReplicatedStorage._0xS0URC4X.Client.InventoryHandler', Line 9 - method getInventory
21:48:19.642 - Script 'ServerScriptService.MainScript', Line 31
21:48:19.643 - Stack End

InventoryHandler: (Module Script)

local inventory = {}

local inventories = {}

function inventory:getInventory(player)
  if inventories[player] then
	print(type(inventories[player]))
    for i,v in pairs(inventories[player]) do
	  for e,o in pairs(v) do
		return e
	  end
    end
  end
end

function inventory:addPlayer(player, inv)
	if inv then
      inventories[player] = {inv}
	end
end

function inventory:addItem(player, itemName, quantity, category, tradeable)
  if inventories[player] then
    table.insert(inventories[player], {
	  [itemName] = {
	    Quantity = quantity,
	    ItemName = itemName,
		Category = category,
		Tradeable = tradeable
	  }
	});
  end
end

function inventory:removePlayer(player)
	inventories[player] = nil
end

return inventory

Server Script:

local dataStoreService = game:GetService("DataStoreService")
local replicatedStorage = game:GetService("ReplicatedStorage")

local replicatedClient = replicatedStorage["_0xS0URC4X"]["Client"]
local inventoryHandler = require(replicatedClient["InventoryHandler"])

local inventoryData = dataStoreService:GetDataStore("inv-sys-1.54")

game.Players.PlayerAdded:Connect(function(player)
 local success, data = pcall(inventoryData.GetAsync, inventoryData, player.UserId)
  if data then
	print("Data loaded for: ".. player.Name)
	inventoryHandler:addPlayer(player.Name, data)
	print(inventoryHandler:getInventory(player.Name))
   else
    print("Creating data for: ".. player.Name)
    inventoryHandler:addPlayer(player.Name, {
	  ["Shovel"] = {
		Quantity = 1,
        ItemName = "Shovel",
        Category = "Tool",
        Tradeable = false 
	  }
    });
	print(inventoryHandler:getInventory(player.Name))
  end
end)

game.Players.PlayerRemoving:Connect(function(player)
  pcall(inventoryData.UpdateAsync, inventoryData, player.UserId, function()
    return inventoryHandler:getInventory(player.Name)	
  end)
  
  inventoryHandler:removePlayer(player.Name)
end)

1 answer

The error is created at Line 13 of the ServerScript.

Because you are inserting a table, with a string inside of it, when you are going to make a pairs() loop within the inventory, it won't work because it is a string, not a table.

You did this:

inventoryHandler:addPlayer(player.Name, data)

However, something like this will work instead:

inventoryHandler:addPlayer( {player.Name} , data)

That's because in the ModuleScript, you are telling the interpreter to iterate (go in pairs() loop) through inventoryHander (a table) and then doing it again, which in the first case, it is a string not a table. That's why it errors.

1 comment

0

There are no errors now, however the inventory isn't being saved now.

This discussion is preserved as it was. New answers, comments, and votes are closed.