I am trying to make the datastore save the players time and it loads in data fine but when the player leaves most of the time it does not save.

local m = require(workspace:WaitForChild("MainModule"))
local DSS = game:GetService("DataStoreService")
local timeStore = DSS:GetDataStore("PlayerTime_v1")

game.Players.PlayerAdded:Connect(function(player)
	local success, playerTime = pcall(function()
		return timeStore:GetAsync("UID_"..player.Name)
	end)
	
	if success then
		print("Succefully loaded data!")
		m.currentTime = playerTime
		while wait(1) do
			m.currentTime = m.currentTime + 1
			print(m.currentTime)
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, err = pcall(function()
		timeStore:SetAsync("UID_"..player.Name,m.currentTime)
	end)
	
	if success then
		print("Data saved!")
	end
end)

2 comments

0

Are you testing this live or on studio?

0

studio