So I am trying to save player data by using SetAsync within a Players.PlayerRemoving script, but it doesn’t actually go into the code and do stuff. It merely stops there and the code that comes after it doesn’t run either. So my question is, why doesn’t datastore saving work while the player is just about to leave and how can I save data in this way because it’s crucial for my anti-server hopping script to have that data saved right before they leave. Thank you.
Can we your script that you managed to make?
It’s literally like this:
local key1 = “user_inventory” … player.userId
print("key 1 is: " .. key1) DataStore:SetAsync(key1, playerInventory) --saveData(playerInventory, key1) print("SAVING PLAYER DATA UPON LEAVE SUCCESSFUL!")
The output prints the line before SetAsync, "key 1 is: … " but it doesn’t print what comes after that, which is the line in caps lock. So i’m guessing it didn’t run up to that point? Why does it stop?
You can try to use a pcall
game.Players.PlayerRemoving:Connect(function(player) local ID = "Player: " .. player.UserId local success,err = pcall(function() DataStore:SetAsync(ID, playerInventory) end) if success then print("Success") else warn("Failed: " .. err) end end)
And make sure you alrdy used the getdatastore function
I just tried it, it still doesn’t print anything in output after that block of code- so i’m guessing it stopped executing around that point.
do you have the full code? It might not be catching the PlayerRemoving event.
The code is long and extensive, but I assure you that it is contained within the “Players.PlayerRemoving” event, and everything BEFORE that line which is saving data in Datastore executes fine and I can see it in output, but after that there is no further execution of code so it must be that line which is trying to save that is causing direct problems.
I don’t think that PlayerRemoving() and saving data is compatible. Would anyone know if a bindable function would run the code?
Can someone please help me I will credit you in my next game…
Are you trying this in studio? It only saves in game.
Yeah i’m doing play solo mode, is that why it’s not running everything?
If you are doing it in studio it doesn’t save the players data and it wouldn’t run anything afterwards I’m pretty sure.
You should use
:BindToClose() as well as
I would make a function like this:
local function SaveData(player) -- Save players data in here end) Players.PlayerRemoving:Connect(function(player) SaveData(player) end) game:BindToClose(function() for _, player in pairs(Players:GetPlayers()) do SaveData(player) end end)
A setup like this should work.
Publish the game and play it. Make sure enable studio access to api services are on. And double check your code.
Is it a server script, or a local script?
So save everyone’s data when someone leaves?