-
What do you want to achieve?
Save to DataStore using the PlayerRemoving event. -
What is the issue?
Data does not save to DataStore in game. -
What solutions have you tried so far?
I have tried to add a BindToClose event. This has caused my game to save to DataStore in Studio, but not in game.
I am trying to store CFrame X, Y, and Z data into DataStore so that the player can start off where they left off. I am testing by trying to save hardcoded values.
The loading of data using PlayerAdded works fine.
For some reason, the SetAsync() in PlayerRemoving wasn’t completing when the player leaves in game or in studio. The weird thing is that when I added BindToClose, the SetAsync() finishes running when in Studio but not in game.
If anyone can tell me why, I’d be very grateful.
Someone suggested that I add a wait(20) to BindToClose, but that didn’t solve the issue.
local function save(plr)
local data = {
CFrameX = -1232;
CFrameY = 1232;
CFrameZ = -1235;
}
local success, errormessage = pcall(function()
MandoSimStore:SetAsync(plr.UserId.."-Location_StorageData", data)
end)
if success then
print("Successfully saved data!")
else
warn("ERROR: "..errormessage)
end
end
local gameShutDown = false
game.Players.PlayerRemoving:Connect(function(plr)
wait(0.1)
if not gameShutDown then
save(plr)
end
end)
game:BindToClose(function()
gameShutDown = true
for _, plr in ipairs(game.Players:GetPlayers()) do
save(plr)
end
wait(20)
end)