Hello, so I've been trying to save upon playerremoving but it never wants to work. I even used BindToClose() and I know it works 30 seconds before shutdown they said. But when I leave, the tool wont save.
This is currently my code right now:
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("BackpackSave")
game.Players.PlayerAdded:Connect(function(player)
pcall(function()
local tool = dataStore:GetAsync("User-"..player.UserId)
if tool then
for i,v in pairs(tool) do
local toolFound = game.ReplicatedStorage.Items:FindFirstChild(v)
if toolFound then
toolFound:Clone().Parent = player.Backpack
end
end
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
pcall(function()
local toolsSave = {}
for i, tool in pairs (player.Backpack:GetChildren()) do
if tool then
table.insert(toolsSave,tool.Name)
end
end
dataStore:SetAsync("User-"..player.UserId,toolsSave)
end)
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do -- notice that you can loop through every player left in the server so the function can run for them
local toolsSave = {}
for i, tool in pairs (player.Backpack:GetChildren()) do
if tool then
table.insert(toolsSave,tool.Name)
end
end
dataStore:SetAsync("User-"..player.UserId,toolsSave)
end
end)
Any help is appreciated...

1 comment
https://developer.roblox.com/en-us/articles/Datastore-Errors Wait 6 seconds between each request