Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local inventory = {}
- local function addItems(player, itemName)
- if type(inventory[player]) == "table" then
- inventory[player][#inventory[player]+1] = itemName
- else
- inventory[player] = {}
- inventory[player][#inventory[player]+1] = itemName
- end
- end
- local function removeItems(player, itemName)
- if type(inventory[player]) == "table" then
- for k,v in pairs(inventory[player]) do
- if v == itemName then
- inventory[player][k] = nil -- if this doesnt work, do:
- -- inventory[player][itemName] = nil
- break
- end
- end
- end
- end
- local DS = game:GetService("DataStoreService")
- local inventoryStore = DS:GetDataStore("Inventories")
- local function loadInventory(player)
- if player.UserId < 1 then return end -- dont load or save for guests.
- local success, loaded = pcall(function()
- local didLoad = false
- local getId = tonumber(player.UserId) or 0
- inventoryStore:UpdateAsync("user-"..getId,function(storedData)
- local manageData = storedData or {}
- inventory[player] = manageData
- didLoad = true
- return manageData
- end)
- return didLoad
- end)
- if success and loaded then
- print("Successfully loaded inventory for: "..tostring(player))
- else
- warn("Error loading inventory for: "..tostring(player))
- end
- end
- local function saveInventory(player)
- if player.UserId < 1 then return end -- dont load or save for guests.
- local success, saved = pcall(function()
- local didSave = false
- local getId = tonumber(player.UserId) or 0
- inventoryStore:UpdateAsync("user-"..getId,function(storedData)
- local manageData = storedData or {}
- manageData = inventory[player]
- didSave = true
- return manageData
- end)
- return didSave
- end)
- if success and saved then
- print("Successfully saved inventory for: "..tostring(player))
- else
- warn("Error saving inventory for: "..tostring(player))
- end
- end
- local itemStorage = game.ServerStorage.DataType
- local function applyInventory(player)
- if type(inventory[player]) == "table" then
- for k,v in pairs(inventory[player]) do
- if itemStorage:FindFirstChild(v) then
- itemStorage[v]:Clone().Parent = player.Backpack
- end
- end
- end
- end
- game.Players.PlayerAdded:Connect(function(player)
- inventory[player] = {}
- loadInventory(player)
- applyInventory(player)
- end)
- game.Players.PlayerRemoving:Connect(function(player)
- saveInventory(player)
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement