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

Data does not save when server is shut down?

Data save works fine, but whenever the server is shutdown, the stats do not save.

local DataStoreService = game:GetService("DataStoreService")
local inventory = DataStoreService:GetDataStore("Inventory")
local values = game.ReplicatedStorage.ShopValues.FirstWalkspeed



	




game.Players.PlayerAdded:Connect(function(player)
	local Data
	local success, err = pcall(function()
		Data = inventory:GetAsync(player.UserId.."-WalkSpeed", values.Value)
	end)
	if success then
		values.Value = Data
	else
			print("Error getting data")
			warn(err)
	end
	
	
end)


game.Players.PlayerRemoving:Connect(function(player)
	
	
	local success, err = pcall(function()
		inventory:SetAsync(player.UserId.."-WalkSpeed", values.Value)
	end)
	
	if success then
		print ("data saved")
	else
			print("There was an error")
			warn(err)
	end
	
end)

5 comments

0

Is api service on?

0

Which line doesnt work?

0

Yeah everythings on, does it need a BindToClose?

0

Yes, it needs a BindToClose.

0

PlayerRemoving doesn't fire when the server is closing because Roblox is Bad™. Use a data cache system and on bindtoclose save any lingering player data.

1 answer

game.BindToClose:Connect(function()
	for _,player in pairs(game:GetService("Players"):GetPlayers()) do
		inventory:SetAsync(player.UserId.."-WalkSpeed",values.Value)
	end
end)

This code will run whenever the game shuts down unexpectedly or even if it is expected.

You don't need to pcall the saving part for this since either way it'll save the player's data.

Hope this helped! Feel free to select this as an answer if this worked for you!

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