hi, ive been trying to solve this one issue for around a week: PlayerRemoving and BindToClose saving doesn’t seem to work for me. my (test) script is below, and for some reason the increase in x.Value += 1 only seems to happen around 40% of the time, completely at random. however, if i include a print line after it it will still print.
things ive tried to rule out:
- nothing wrong with the GetAsync, manually tested by setting it with a different script and Get worked
- references seem okay, at least each has been tested
- if I run SetAsync elsewhere it also works. just the specific combination of it being in PlayerRemoving and BindToClose
- i have tested both removing and bind separately, both appear the same, where the += 1 works sometimes but not others
- i have turned off vpn, and my connection is pretty fast
- have followed the advice from these posts: [SOLVED!] PlayerRemoving function doesn't always work and How to properly save player data in data stores upon server close, neither worked
- for the script below it is the only script in a blank world, so its not interference from others
if you test this and it works (i.e. print line upon joining increases each time) please say, then itd probably be a problem only on my laptop lol
local players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("PlayerDataStore")
game.Players.PlayerAdded:Connect(function(player)
local x = Instance.new("IntValue", player)
x.Name = "x"
x.Value = DataStore:GetAsync(player.UserId) or 1
print (x.Value)
end)
game.Players.PlayerRemoving:Connect(function(player)
local x = player:FindFirstChild("x")
if x then
x.Value += 1
print("fired")
local success, err = pcall(function()
DataStore:SetAsync(player.UserId, x.Value)
end)
if success then
print("saved")
else
print("failed: " .. tostring(err))
end
end
end)
game:BindToClose(function()
for _, player in pairs(players:GetPlayers()) do
local x = player:FindFirstChild("x")
if x then
local success, err = pcall(function()
DataStore:SetAsync(player.UserId, x.Value)
end)
if not success then
warn("failed for " .. player.Name .. ": " .. tostring(err))
end
end
end
end)
sorry if im submitting this wrong, always open to feedback ^^
edit - will be testing out OtherWRLDly’s solution and then updating when i can. the weirdest thing about this all is that this exact format of saving script genuinely worked no flaws a few months ago, but i havent seen any relevant updates to the async after searching so far,