I run this function on playerremoving, and the only result I get is the printed “odd.” I want my datastore to save, and I’ve been using this datastore for months with no issue.
I’ve tried printing the success and response before the ifs, to no avail.
local function saveData(Key)
print("odd")
local success, response = pcall(function()
dataStore:UpdateAsync(Key, function() return Data end)
end)
if success then
print("No errors, data saved!")
else
print("Data couldn't be saved!")
warn(response)
end
end
Studio sometimes has a weird thing with saving data on server close. It’s possible that this code isn’t running because when the player leaves, the game immediately closes and the function doesn’t run (in studio).
Something I like to do (and could be helpful in games) is using a BindToClose function, which runs when the server is shut down. By stopping the play test mode in Studio, this imitates the server being shut down.
game:BindToClose:Connect(function()
wait(1)
for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
local playerKey = player.UserId .. "WHATEVER KEY YOU USE HERE"
coroutine.wrap(saveData)(playerKey)
end
end)