Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Datastorage does not save data when leaving, how to fix?

I know someone else has asked this but it hasn't been answered and I'm really stuck and worried I won't be able to find it again. So sometimes my data stores work and sometimes they don't. It's weird. Here's the important code and I'm not getting any errors or errormessage

game.Players.PlayerRemoving:Connect(function(player)
	local Id = "player_"..player.UserId
	local Data = player.leaderstats.Cash.Value
	
	local success, errormessage = pcall(function()
		DataStore1:SetAsync(Id, Data)
		print("DataSaved")
	end)
	
	if success then
		print("DataSaved")
	elseif errormessage then
		print("There was a error")
		warn(errormessage)
	end
end)

3 comments

0

Sorry. I mix things up easily.. I dont know what is wrong with your code then.

0

nah it's fine

0

if you are testing the script in studio, try to test your game in the roblox's website instead

1 answer

✓ Accepted answer

If you are testing and you are the only one in you server, the server shuts down when you leave and the code wont run. To prevent this, add another block of code that includes this

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do

		local Id = "player_"..player.UserId
  		local Data = player.leaderstats.Cash.Value

    		local success, errormessage = pcall(function()
        	DataStore1:SetAsync(Id, Data)
        		print("DataSaved")
    		end)

   		if success then
        		print("DataSaved")
		elseif errormessage then
			print("There was a error")
warn(errormessage)
		end
	end
end)

Put this under your other code

1 comment

0

thanks:)

This discussion is preserved as it was. New answers, comments, and votes are closed.