How to make time spent on game?

How to make time spent on game, like 1 hour and minutes? I’m quite confused.

My script:

--// Variables, Services and functions

local DataStoreService = game:GetService("DataStoreService")

local TimeData = DataStoreService:GetOrderedDataStore("TimeData")

local Players = game:GetService("Players")

local gameHours = 0

local gameMinutes = 0

local gameSeconds = 0

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

	     local Teir = Instance.new("StringValue")
	     Teir.Name = "Teir"
	     Teir.Value = "o"
	     Teir.Parent = leaderstats
	
         local timeSpent = Instance.new("StringValue")
	     timeSpent.Name = "Time"
	     timeSpent.Value = gameHours..":"..gameSeconds
	     timeSpent.Parent = leaderstats
	
	     local playerData
	     local sucess, err = pcall(function()
		   playerData = TimeData:GetAsync(player.UserId.." -Time")
		   
	end)
	
	     if sucess then
		   timeSpent.Value = playerData
	else
			warn("Error saving player data!")
	end
	
end)

Players.PlayerRemoving:Connect(function(player)
	    
	    local success, err = pcall(function()
		   TimeData:GetAsync(player.UserIDd.." -Time",player.leaderstats.Time.Value)
	end)
	
	if success then
		   print("Data Saved!")
	else
			warn("Data not saved!")
	end
	
end)

      while true do wait(1)
		   gameSeconds = gameSeconds + 1
	       gameMinutes = gameSeconds * 60
	       gameHours = gameMinutes * 60
	        timeSpent.Value = gameHours..":"..gameSeconds
	end

My time value just prints 36000 and stuff like that, It’s my first time implementing this. Thanks in advance! :smiley:

I didn’t really read through the code, but here is how to save the time spent in a game.

--on a server script of course

local starttime = os.time() --player joins the game

local endtime = os.time() --player leaves game
local totaltime = endtime - starttime --total time in seconds 
--do data store stuff
1 Like

Do I need to use while loop for that? Also, how about hours and minutes?

No loop is needed.

To get minutes, divide by 60, divide more as needed for other values

1 Like

And for hours, divide minutes by 60?

Hate to say this but I don’t seem to understand. :frowning:

--// Variables, Services and functions

local DataStoreService = game:GetService("DataStoreService")

local TimeData = DataStoreService:GetOrderedDataStore("TimeData")

local Players = game:GetService("Players")

local gameHours = 0

local gameMinutes = 0

local gameSeconds = 0

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

	     local Teir = Instance.new("StringValue")
	     Teir.Name = "Teir"
	     Teir.Value = "o"
	     Teir.Parent = leaderstats
	
         local timeSpent = Instance.new("StringValue")
	     timeSpent.Name = "Time"
	     timeSpent.Value = gameHours..":"..gameSeconds
	     timeSpent.Parent = leaderstats
	
	     local startTime = os.time() 
	
	     local endTime = os.time()  
	
	     local Seconds = endTime - startTime
	     
	     local Minutes = endTime - startTime / 60

             ```

I just simply copied your script. :frowning:

Yes

seconds = 239834 --some number of seconds (for the example)
hours = seconds / 60 / 60
1 Like

endtime will need to be set only when the player leaves the game

I have to go now, I will be back.

1 Like

Alright, thanks though! ( 30 chars)