I'm having an issue coding my inventory system. I'm getting the error: 14:28:06.262 - ServerScriptService.InventoryManager:123: attempt to index local 'inventory' (a nil value) when the first player who joins the game gets an item. Help?
local HttpService = game:GetService("HttpService")
local ReplicatedStorage = game.ReplicatedStorage
--Useful function(s)
function isInTable(tableValue,findValue)
local found = false
for k,v in pairs(tableValue) do
if tableValue[k] == findValue then
if k ~= nil then
found = k
end
end
end
return found
end
function shallowCopy(original)
local copy = {}
for key, value in pairs(original) do
copy[key] = value
end
return copy
end
--Server
itemId = {1,2,3,4}
itemName = {"Iron","Gold","Doubloons","Copper"}
inventory = {
Player = nil,
ItemId = {},
ItemName = {},
ItemAmount = {},
HotbarId = {},
HotbarName = {},
HotbarAmount = {},
}
inventories = {}
-- function inventory:new(playerName,object)
-- local object = object or {}
-- setmetatable(object, self)
-- self.__index = self
-- self.Player = playerName
-- table.insert(inventories,object)
-- return object
-- end
function new(playerName,object)
if object == nil then
object = {}
end
local newObject = inventory
newObject.Player = playerName
table.insert(inventories,newObject)
return newObject
end
function changed(playerName)
print("Client Fired")
ReplicatedStorage.Events.InventoryChange:FireClient(game.Players[playerName])
end
function getInventory(playerName)
for i,v in pairs(inventories) do
local currentName = inventories[i].Player
if currentName == playerName then
local inventory = inventories[i]
--local inventoryJSON = HttpService:JSONEncode(inventory)
return inventory
end
end
end
function getInventoryId(playerName)
for i,v in pairs(inventories) do
local currentName = inventories[i].Player
if currentName == playerName then
return i
end
end
end
function numberOf(playerName,id)
local inventory = inventories[getInventoryId(playerName)]
local numberOf
if isInTable(inventory.ItemId,id) == false then
numberOf = 0
else
local itemKey = isInTable(inventory.ItemId,id)
numberOf = inventory.ItemAmount[itemKey]
end
return numberOf;
end
function addTo(playerName,id,amount)
local inventory = inventories[getInventoryId(playerName)]
if isInTable(inventory.ItemId,id) == false then
table.insert(inventory.ItemId,itemId[id])
table.insert(inventory.ItemName,itemName[id])
table.insert(inventory.ItemAmount,amount)
else
local numberOf = numberOf(playerName,id)
inventory.ItemAmount[id] = inventory.ItemAmount[id] + amount
end
changed(playerName)
end
function removeFrom(playerName,id,amount)
local key
local inventory = inventories[getInventoryId(playerName)]
local numberOfItem = inventory.numberOf(playerName)
if numberOfItem >= amount then
if isInTable(inventory.ItemId,id) ~= false then
key = isInTable(inventory.ItemId,id)
inventory.ItemAmount[key] = numberOfItem - amount
end
end
if numberOfItem <= amount then
inventory.ItemAmount[key] = 0
end
changed(playerName)
end
game.Players.PlayerAdded:Connect(function(player)
print('new')
new(player.Name)
end)
game.Players.PlayerRemoving:Connect(function(player)
local inventoryId = getInventoryId(player.Name)
table.remove(inventories,inventoryId)
end)
game.ServerStorage.Events.AddItem.Event:Connect(function(playerName,id,amount)
addTo(playerName,id,amount)
end)
game.ServerStorage.Events.RemoveItem.Event:Connect(function(playerName,id,amount)
removeFrom(playerName,id,amount)
end)
--wait(4)
--print(inventory:addTo("chasedig1",1,8459))
--print(inventory:getInventory("chasedig1"))
--print(inventory:numberOf("chasedig1",1))
----Remote
function getOwnInventory(player)
local inventory = getInventory(player.Name)
return inventory
end
ReplicatedStorage.Events.GetOwnInventory.OnServerInvoke = getOwnInventory

4 comments
epic.
u cant define the inventory, thats on the client side or with other words can only be done with a local script
Ik
Please indent the code so that it's possible to read.