How do i go about saving the time played?

how do i go about on saving the players time played stats cause when player dies ingame it resets back to zero and when they rejoin, and i’m trying to not cause that to happen i’m trying to keep the saved data aka the saved remaining time played stats that are showing but idk how other than making a leaderstat and saving that instead and using those numbers to display to the gui. but is there another way without needing to do that and just use this script? if not i’ll just do my method.

local startTime = tick() -- Use tick() instead of os.time()
local DataStoreService = game:GetService("DataStoreService")
local TimePlayedDataStore = DataStoreService:GetDataStore("Play333333")

-- Function to convert seconds to a formatted time string
local function formatTime(timeInSeconds)
	local minutes = math.floor(timeInSeconds / 60)
	local seconds = math.floor(timeInSeconds % 60)
	local hours = math.floor(minutes / 60)
	local days = math.floor(hours / 24)
	local months = math.floor(days / 30)
	local years = math.floor(months / 12)

	return string.format("%d years, %d months, %d days, %02d:%02d:%02d", years, months % 12, days % 30, hours % 24, minutes % 60, seconds)
end

-- Function to update the UI with the player's playtime
local function updatePlayTimeUI()
	local currentTime = tick()
	local timePlayed = currentTime - startTime
	PlayTime.Text = formatTime(timePlayed)
end

-- Function to load and display player's total playtime when they join the game
local function onPlayerAdded(player)
	local playerKey = "Player_" .. player.UserId
	local savedTime = TimePlayedDataStore:GetAsync(playerKey)

	if savedTime then
		-- Update the startTime based on the saved playtime
		startTime = tick() - savedTime
		-- Update the UI to show the saved playtime
		updatePlayTimeUI()
	else
		-- If there's no saved time or an error occurred, set the startTime to the current time
		startTime = tick()
	end
end

game.Players.PlayerAdded:Connect(onPlayerAdded)

-- Update the UI every second
game:GetService("RunService").Heartbeat:Connect(updatePlayTimeUI)

game.Players.PlayerRemoving:Connect(function(player)
	-- Calculate the total time played
	local currentTime = tick()
	local timePlayed = currentTime - startTime

	-- Save the total playtime in the DataStore
	local playerKey = "Player_" .. player.UserId
	local success, errorInfo = pcall(function()
		local savedTime = TimePlayedDataStore:GetAsync(playerKey) or 0
		TimePlayedDataStore:SetAsync(playerKey, savedTime + timePlayed)
	end)

	if not success then
		warn("Error while saving playtime for player " .. player.Name .. ": " .. errorInfo)
	end
end)
5 Likes

any help???

1 Like

You are doing it kinda wrong. You are using a single variable called startTime to store the time when a player joined but since this is the server every time a player joins, this value will get overwritten.

Also, don’t update the player’s gui from server. Just fire an RE to the client asking them to start the timer.

Lastly, use IncrementAsync to update the time.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.