-----------------------------------------------| Save Stats |------------------------------------------------------------



local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")


game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Time = Instance.new("IntValue")
	Time.Name = "Time"
	Time.Parent = leaderstats

	local playerUserId = "Player"..player.UserId

	local data 

	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(playerUserId) 
	end)


	if success then
		Time.Value = data  
	end

	while wait(1) do
		player.leaderstats.Time.Value = player.leaderstats.Time.Value + 0 --Don't change it or it will make it go up twice
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player"..player.UserId

	local data = player.leaderstats.Time.Value

	myDataStore:SetAsync(playerUserId, data)   

end)
