Attempt to index nil with number

Yeah I can do that if you want to provide a file.

InvSystemIssue.rbxl (66.5 KB)

1 Like

Think I got it. Is this how you want it to look?

Screen Shot 2021-03-13 at 8.27.05 PM

If so, here is what I came up with:

local itemModule = require(game:GetService("ReplicatedStorage"):WaitForChild("ItemModule"))
local repStorage = game:GetService("ReplicatedStorage")
local remotes = repStorage:WaitForChild("Remotes")
local AddItemEvent = remotes:WaitForChild("AddItem")
local refreshInv = remotes:WaitForChild("Refresh")
local Players = game:GetService("Players")
local Interactables = workspace:WaitForChild("Interactables")

invsize = 20
local playerInventories = {}

local function getEmptySlot(inventory)
	for i = 1, invsize do
		if inventory[i] == nil then -- If the index inside of the inventory doesn't exist then return the index as it's the first available slot inside of the inventory
			return i
		end
	end
end

local function findItemSlot(inventory, item)
	for i,v in pairs(inventory) do
		if v[item] then -- If the index and the item inside of the index exists, return the index. Otherwise return nil (which happens automatically, no need for "or nil")
			return i
		end
	end
end

local function AddItem(plr, item, amt)
	local itemType = itemModule[item]["Type"]
	local playerInventory = playerInventories[plr]
	playerInventory[itemType] = playerInventory[itemType] or {} -- If the itemType doesn't exist within the player's inventory, create a new table for the itemType
	local inventoryItemType = playerInventory[itemType] -- Get the new table
	local itemExists = findItemSlot(inventoryItemType, item) -- Find the index in the table
	if not itemExists then -- If the index doesn't exist then create a new table containing the item's information
		inventoryItemType[getEmptySlot(inventoryItemType)] = {[item] = amt}
	else -- Otherwise, increment the value by 1
		inventoryItemType[itemExists][item] += 1
	end
	print(playerInventory)
end

--Layout of table
--playerInventories[plr][Section][slot][item][amt]

Players.PlayerAdded:Connect(function(player)
	playerInventories[player] = {}
end)

AddItemEvent.OnServerEvent:Connect(function(plr, item)
	local mag = (item.Position - plr.Character.HumanoidRootPart.Position).Magnitude
	local amt = 1
	if item then
		if mag <= 10 then
			AddItem(plr, item.Name, 1)
			refreshInv:FireClient(plr, item, amt)
			item:Destroy()
		end
	end
end)
1 Like

thanks for your help! really appreciate it

1 Like