Hello! I’ve tried my best the past couple of days in saving a player’s inventory, for some reason, no matter what I try, Roblox just decides to not save it sometimes (not all of the time).
Could anyone help?
local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerSave3")
local mps = game:GetService("MarketplaceService")
local http = game:GetService("HttpService")
local debounce = true
game.Players.PlayerAdded:Connect(function(plr)
task.wait(5)
local inventory = {}
local leaderstats = Instance.new('Folder')
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local gems = Instance.new("NumberValue")
gems.Name = "Dollars"
gems.Parent = leaderstats
local itemFolder = Instance.new('Folder')
itemFolder.Name = "ItemFolder"
itemFolder.Parent = plr
local gemsData
local success,errormessage = pcall(function()
gemsData = DataStore:GetAsync("Dollars"..plr.UserId)
inventory = http:JSONDecode(DataStore:GetAsync("Inventory"..plr.UserId))
print("Printing "..plr.Name.."'s inventory!")
for i, v in pairs(inventory) do -- this sometimes doesn't print, meaning it either didn't save or didn't load
print(v)
end
end)
if success then
if gemsData then
gems.Value = gemsData
end
if inventory then
repeat wait() until plr.Team ~= game:GetService("Teams"):WaitForChild("Unemployed")
local invFolder
if plr.Team ~= nil then
plr.PlayerGui.Buttons.Warning.Text = "Loading Data"
task.wait(1.5)
invFolder = game:GetService("ServerStorage"):FindFirstChild(tostring(plr.Team.Name))
plr.PlayerGui.Buttons.Warning.Text = "Successfully loaded data!"
wait(1)
plr.PlayerGui.Buttons.Warning.Text = ""
end
if invFolder then
for i, v in pairs(inventory) do
if invFolder:FindFirstChild(v) then
invFolder[v]:Clone().Parent = workspace.Tycoons:FindFirstChild(tostring(plr.Team.Name)).PurchasedObjects
invFolder[v]:Clone().Parent = itemFolder
for _, object in pairs(workspace.Tycoons[tostring(plr.Team.Name)].Buttons:GetChildren()) do
if workspace.Tycoons[tostring(plr.Team.Name)].PurchasedObjects:FindFirstChild(object.Object.Value) then
object:Destroy()
end
end
end
end
end
end
else
plr:Kick("Data failed to load, rejoining is recommended. If this issue persists, contact @basicallyrbx on Twitter.")
end
-- ADD ITEMS TO THE INVENTORY TABLE
coroutine.resume(coroutine.create(function()
while wait() do
if plr then
if plr.Team ~= nil then
if plr.Team ~= game:GetService("Teams"):WaitForChild("Unemployed") then
workspace:WaitForChild("Tycoons"):FindFirstChild(tostring(plr.Team.Name)).PurchasedObjects.ChildAdded:Connect(function(child)
if debounce == true then
if not table.find(inventory,child.Name) then
debounce = false
table.insert(inventory,child.Name)
child:Clone().Parent = itemFolder
task.wait(.25)
debounce = true
end
end
end)
end
end
end
end
end))
-- SAVE DATA EVERY 60 SECONDS
coroutine.resume(coroutine.create(function()
while wait(60) do
if plr.Team ~= nil then
if plr.Team ~= game:GetService("Teams"):WaitForChild("Unemployed") then
local data = http:JSONEncode(inventory)
local success, errormsg = pcall(function()
DataStore:SetAsync("Dollars"..plr.UserId,plr.leaderstats["Dollars"].Value)
DataStore:SetAsync("Inventory"..plr.UserId,data)
end)
if success then
plr.PlayerGui.Buttons.Warning.Text = "Saved Data"
wait(1.5)
plr.PlayerGui.Buttons.Warning.Text = ""
else
plr.PlayerGui.Buttons.Warning.Text = "Failed to save data, try again in 1 minute"
wait(2.5)
plr.PlayerGui.Buttons.Warning.Text = ""
end
end
end
end
end))
end)
-- SAVE ON PLAYER LEAVING
game.Players.PlayerRemoving:Connect(function(player)
local inventory = {}
for i, v in pairs(player.ItemFolder:GetChildren()) do
if not table.find(inventory,v.Name) then
table.insert(inventory, v.Name)
end
end
local data = http:JSONEncode(inventory)
local success, errormessage = pcall(function()
DataStore:SetAsync("Dollars"..player.UserId,player.leaderstats["Dollars"].Value)
DataStore:SetAsync("Inventory"..player.UserId,data)
end)
end)
-- SAVE ON GAME CLOSING
game:BindToClose(function()
for i, plr in pairs(game:GetService("Players"):GetPlayers()) do
local inventory = {}
for i, v in pairs(plr.ItemFolder:GetChildren()) do
if not table.find(inventory,v.Name) then
table.insert(inventory, v.Name)
end
end
local data = http:JSONEncode(inventory)
local success, errormsg = pcall(function()
DataStore:SetAsync("Dollars"..plr.UserId,plr.leaderstats["Dollars"].Value)
DataStore:SetAsync("Inventory"..plr.UserId,data)
end)
end
end)