โ† Back to all scripts

Inventory System

Server-side inventory using a table. Add, remove, and check items.

EconomyIntermediateServerScriptService
inventory-system.lua
-- Inventory System
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local inventories = {}

local getInvRF = Instance.new("RemoteFunction")
getInvRF.Name = "GetInventory"; getInvRF.Parent = ReplicatedStorage

game.Players.PlayerAdded:Connect(function(p) inventories[p.UserId] = {} end)
game.Players.PlayerRemoving:Connect(function(p) inventories[p.UserId] = nil end)

local function addItem(player, itemId, qty)
    local inv = inventories[player.UserId]; if not inv then return end
    inv[itemId] = (inv[itemId] or 0) + (qty or 1)
end

local function removeItem(player, itemId, qty)
    local inv = inventories[player.UserId]; if not inv or not inv[itemId] then return false end
    qty = qty or 1; if inv[itemId] < qty then return false end
    inv[itemId] = inv[itemId] - qty
    if inv[itemId] <= 0 then inv[itemId] = nil end
    return true
end

getInvRF.OnServerInvoke = function(player)
    return inventories[player.UserId] or {}
end
Related scripts in Economy
Shop SystemIntermediate โ†’Give Cash on KillBeginner โ†’
Something broken or not working?
๐Ÿ”ง Try the AI Script Fixer โ†’