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...
your character is probably spawning after you parent the tool to the backpack, meaning that your backpack gets reset
try parenting a clone of the tool to both player.Backpack and player.StarterGear instead of just player.Backpack
It worked fine for me. Are you sure you have API Services enabled? (Game Settings -> Security -> Enable Studio Access to API Services)? Make sure to restart your studio after you save.