'Time Survived' Help

Hi!

I am trying to make a game where you jump in a hole which teleports you to the map and from then, your time survived is tracked in a leaderstat until you die or leave the game. Does anyone know how to possibly do this? Google gives me nothing. Thanks

1 Like

Store the tick when you first join the game/when you want the clock to start with os.clock(), and when calculating the time passed you can do os.clock() - startClock

Using os.clock() instead of tick() ensures that when you change your computer / phone time, the timer ingame doesn’t change.

How exactly can I do that? Sry, I’m not that good at scripting. (I want to start the clock when a part is touched btw)

(and end it when you die or leave obv)

On a server script:

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local Part = -- Your part here

local PlayerTicks = {}

local function Touched(OtherPart)
    local Character = OtherPart.Parent
    if not Character then return end

    local Player = Players:FindFirstChild(character.Name)
    if not Player then return end

    PlayerTicks[Player] = os.clock()

    Character.Humanoid.Died:Once(function()
        PlayerTicks[player] = nil
    end)
end
local function PlayerRemoving(player)
    PlayerTicks[player] = nil
end
local function UpdateAllPlayers()
    for _,Player in pairs(Players:GetChildren()) do
        Player.leaderstats["Time Survived"].Value = if PlayerTicks[Player] then os.clock() - PlayerTicks[Player] else 0
    end
end

Part.Touched:Connect(Touched)
Players.PlayerRemoving:Connect(PlayerRemoving)
RunService.Heartbeat:Connect(UpdateAllPlayers)

Usually I wouldn’t provide an entire script, as most of the point of scripting is to make it your own, but this was a relatively short script so I didn’t mind.

To start counting when a part is touched, you can do the following:

local PLS = game:GetService('Players')
local players = {} -- will track each player's timers

PLS.PlayerAdded:Connect(function(Player)
   players[Player.Name] = {
      StartTime = 0,
      TotalTime = 0,
      PartTouched = false
   }

   local leaderstats = Instance.new('Folder')
   leaderstats.Name = 'leaderstats'
   leaderstats.Parent = Player

   local timer = Instance.new('IntValue')
   timer.Name = 'Timer'
   timer.Parent = leaderstats

   local character = Player.Character or Player.CharacterAdded:Wait()
   local humanoid: Humanoid = character.Humanoid

   humanoid.Died:Connect(function()
      if not players[Player.Name].PartTouched then return end
      players[Player.Name].TotalTime = os.clock() - players[Player.Name].StartTime
      Player.leaderstats.Timer.Value = players[Player.Name].TotalTime
      players[Player.Name].PartTouched = false
   end)
end)

local part: BasePart = thePart -- the part to be touched
local timer: number -- variable that will store time when part is touched

part.Touched:Connect(function(Hit)
   local player = PLS:GetPlayerFromCharacter(Hit.Parent)
   if players[player.Name].PartTouched then return end -- return if part already touched by player

   players[player.Name].Timer = os.clock() -- store time when part is touched
   players[player.Name].PartTouched = true -- set part touched to true
end)

PLS.PlayerRemoving:Connect(function(Player)
   players[Player.Name].TotalTime = os.clock() - players[Player.Name].StartTime
   Player.leaderstats.Timer.Value = players[Player.Name].TotalTime
   players[Player.Name] = nil
end)

Place the code in a server script.