saveData function not functioning correctly, it worked fine yesterday

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
1 Like

Idk if this is the issue or not, but when you do the player removing, shouldn’t you do SetAsync() instead of UpdateAsync()?

1 Like

SetAsync should not be used for saving data in any case, updateasync works fine with playerremoving.

Are you testing this in studio?

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)
3 Likes

This worked perfectly, thank you!

1 Like