I know someone else has asked this but it hasn't been answered and I'm really stuck and worried I won't be able to find it again. So sometimes my data stores work and sometimes they don't. It's weird. Here's the important code and I'm not getting any errors or errormessage
game.Players.PlayerRemoving:Connect(function(player) local Id = "player_"..player.UserId local Data = player.leaderstats.Cash.Value local success, errormessage = pcall(function() DataStore1:SetAsync(Id, Data) print("DataSaved") end) if success then print("DataSaved") elseif errormessage then print("There was a error") warn(errormessage) end end)
If you are testing and you are the only one in you server, the server shuts down when you leave and the code wont run. To prevent this, add another block of code that includes this
game:BindToClose(function() for _, player in pairs(game.Players:GetPlayers()) do local Id = "player_"..player.UserId local Data = player.leaderstats.Cash.Value local success, errormessage = pcall(function() DataStore1:SetAsync(Id, Data) print("DataSaved") end) if success then print("DataSaved") elseif errormessage then print("There was a error") warn(errormessage) end end end)
Put this under your other code