Hello I am new to roblox engine and I am trying to make a inventory system, the players inventory is saved in a table and when I put a item in it it deletes some other items to, the Items are identified by the slot, but they keep getting deleted, so please help me. so what is wrong with my code?
Inventory System:
local Inventories = {}
local InventoryHandler = {}
function InventoryHandler:CreateInv(Player)
local Inventory = {}
Inventories[Player] = Inventory
return Inventories[Player]
end
function InventoryHandler:AddItem(Player, ItemData)
if not Inventories[Player] then
Inventories[Player] = {}
end
if Player.ClassName == "Player" then
local pInv = Inventories[Player]
if #pInv < 500 then
local slot = #pInv+1
pInv[slot] = ItemData
pInv[slot].Slot = slot
--gui stuff===============
local itemSlot = game.ReplicatedStorage.GuiTemp.ItemFrame
local NewCopy = itemSlot:Clone()
NewCopy.Parent = Player.PlayerGui:WaitForChild("MainGui"):WaitForChild("InvMain"):WaitForChild("Main"):WaitForChild("ScrollingFrame")
NewCopy.ItemName.Text = pInv[slot].Name
NewCopy.Slot.Value = slot
--==============================
return slot
end
elseif game.Players:FindFirstChild(Player.Parent.Name) then
local rPlayer = game.Players:FindFirstChild(Player.Parent.Name)
local pInv = Inventories[Player]
if #pInv < 500 then
local slot = #pInv+1
pInv[slot] = ItemData
pInv[slot].Slot = slot
--gui stuff===============
local itemSlot = game.ReplicatedStorage.GuiTemp.ItemFrame
local NewCopy = itemSlot:Clone()
NewCopy.Parent = rPlayer:WaitForChild("PlayerGui"):WaitForChild("MainGui"):WaitForChild("InvMain"):WaitForChild("Main"):WaitForChild("ScrollingFrame")
NewCopy.ItemName.Text = pInv[slot].Name
NewCopy.Slot.Value = slot
--==============================
return slot
end
else
print("Can't Find Player.")
end
end
function InventoryHandler.getItem(Player, Slot, get)
if not Inventories[Player] then
InventoryHandler:CreateInv(Player)
end
local pInv = Inventories[Player]
if pInv[Slot] then
if get == "Name" then
return pInv[Slot].Name
elseif get == "Physical" then
return pInv[Slot].Physical
elseif get == "SpellPower" then
return pInv[Slot].SpellPower
elseif get == "LevelReq" then
return pInv[Slot].LevelReq
elseif get == "Upgrades" then
return pInv[Slot].Upgrades
elseif get == "MaxUpgrades" then
return pInv[Slot].MaxUpgrades
elseif get == "SellGold" then
return pInv[Slot].SellGold
elseif get == "Health" then
if pInv[Slot].Health then
return pInv[Slot].Health
else
return nil
end
elseif get == "Rarity" then
return pInv[Slot].Rarity
end
end
end
function InventoryHandler:DeleteInventory(Player)
if Inventories[Player] then
Inventories[Player] = nil
end
end
function InventoryHandler:GetNextSlot(Player)
if not Inventories[Player] then
InventoryHandler:CreateInv(Player)
end
local pInv = Inventories[Player]
return #pInv+1
end
function InventoryHandler:CheckSlots(Player)
if not Inventories[Player] then
InventoryHandler:CreateInv(Player)
end
local pInv = Inventories[Player]
return #pInv
end
return InventoryHandler
When they join:
local InventoryHandler = require(script.Parent.InvSystem)
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
--Items===================
local WoodSword = {
["Slot"] = 0,
["Name"] = "Wood Sword",
["Class"] = "weapon",
["Physical"] = 10,
["SpellPower"] = 1,
["LevelReq"] = 1,
["Upgrades"] = 0,
["MaxUpgrades"] = 10,
["SellGold"] = 100,
["Rarity"] = "Common"
}
local BeginnerWand = {
["Slot"] = 0,
["Name"] = "Beginner Wand",
["Class"] = "weapon",
["Physical"] = 1,
["SpellPower"] = 10,
["LevelReq"] = 1,
["Upgrades"] = 0,
["MaxUpgrades"] = 10,
["SellGold"] = 100,
["Rarity"] = "Common"
}
--========================
--creates the inventory
InventoryHandler:CreateInv(player)
InventoryHandler:AddItem(player, WoodSword)
InventoryHandler:AddItem(player, BeginnerWand)
--=================
end)
Players.PlayerRemoving:Connect(function(player)
InventoryHandler:DeleteInventory(player)
end)
When touching a certain block give item script:
local InventoryHandler = require(game:GetService("ServerScriptService").InvSystem)
local gave = false
local WoodSword = {
["Name"] = "Wood Sword",
["Class"] = "weapon",
["Physical"] = 10,
["SpellPower"] = 1,
["LevelReq"] = 1,
["Upgrades"] = 0,
["MaxUpgrades"] = 10,
["SellGold"] = 100,
["Rarity"] = "Common"
}
script.Parent.Touched:Connect(function(player)
if gave == false then
gave = true
InventoryHandler:AddItem(player, WoodSword)
end
end)
script.Parent.TouchEnded:Connect(function(player)
gave = false
end)
Thank You!

1 comment
Use DataStoreService:SetAsync to save it to the store.