I am trying to save a table once the player leaves and I do not get any errors but it prints that it fails.
game.Players.PlayerAdded:Connect(function(player)
local data
local success, errormessage = pcall(function()
data = Datastore:GetAsync(player.UserId.."PlayerInventory")
end)
if success then
for i,v in pairs(Inventory.PlayerInventory) do
for i,v in pairs(Inventory.PlayerInventory) do
if v.Name == game.ReplicatedStorage.ItemImages[v.Name].Name then
local newimages = game.ReplicatedStorage.ItemImages[v.Name]:Clone()
newimages.Parent = player.PlayerGui:WaitForChild("Inventory").Frame
end
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
Datastore:SetAsync(player.UserId.."PlayerInventory", Inventory.PlayerInventory)
end)
if errormessage then
print("failed")
warn(errormessage)
end
end)
One thing I noticed is that you’re doing player.Userid instead of player.UserId in the player removed script.
Also, you aren’t printing the error message.
That doesn’t make sense. He shouldn’t use a leaderboard saving system for this, a regular table would do. Any type of leaderstats value would only be cash, or maybe deaths and knockouts. What you want him to do is inefficient in my eyes.
local ds = game:GetService("DataStoreService"):GetDataStore("ToolSave")
game.Players.PlayerAdded:connect(function(plr)
local key = "id-"..plr.UserId
pcall(function()
local tools = ds:GetAsync(key)
if tools then
for _, tool in ipairs(tools) do
local tool = game.ServerStorage.Tools:FindFirstChild(tool)
if tool then
tool:Clone().Parent = plr:WaitForChild("Backpack")
tool:Clone().Parent = plr:WaitForChild("StarterGear")
end
end
end
end)
end)
game.Players.PlayerRemoving:connect(function(plr)
local key = "id-"..plr.userId
pcall(function()
local toolsToSave = {}
for i,v in ipairs(plr.Backpack:GetChildren()) do
if v then
table.insert(toolsToSave,v.Name)
end
end
ds:SetAsync(key,toolsToSave)
end)
end)
I think he is looking to store skins for the axe, pickaxe, and hoe items to save. It could possibly work if you switched a few things around, but still seems a tiny little bit inefficient.
I solved it I looped through twice for some reason.
if success then
for i,v in pairs(Inventory.PlayerInventory) do
for i,v in pairs(Inventory.PlayerInventory) do
if v.Name == game.ReplicatedStorage.ItemImages[v.Name].Name then
local newimages = game.ReplicatedStorage.ItemImages[v.Name]:Clone()
newimages.Parent = player.PlayerGui:WaitForChild("Inventory").Frame
end
end
end
end
I wrote you new functions that will (hopefully) run smoother. This is because your code seems a little messy and most likely might break in the future.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DatastoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local ItemImages = ReplicatedStorage:WaitForChild("ItemImages")
Players.PlayerAdded:Connect(function(Player)
local PlayerGui = Player:WaitForChild("PlayerGui")
local Inventory = PlayerGui:WaitForChild("Inventory")
local Data = nil
local Tries = 0
repeat
Tries = Tries + 1
local Success = pcall(function()
Data = Datastore:GetAsync(string.format("%dPlayerInventory", Player.UserId))
end)
wait(1)
until Success or Tries >= 10
if Data then
for _,v in pairs(Inventory.PlayerInventory) do
local NewImages = ReplicatedStorage:FindFirstChild(v.Name)
if NewImages then
NewImages:Clone().Parent = Inventory
end
end
end
end)
Players.PlayerRemoving:Connect(function(Player)
local Tries = 0
repeat
Tries = Tries + 1
local Success, Error = pcall(function()
Datastore:SetAsync(string.format("%dPlayerInventory", Player.UserId), Inventory.PlayerInventory)
end)
if Error then
warn(string.format("Error: %s", Error))
end
wait(1)
until Success or Tries >= 10
end)
Do make sure your code is clean and you don’t do things like v.Name == Thing[v.Name].Name.