How can I see how long the game has started? (Without loops)

Hello!

While developping my game I decided to see how long did the game last.
Basically, in my game, I have a start up screen, the game starts and the end the player might die or survive.

And when the player wins or die I want to see how long he lasted.

But I thought I could just do this:

StartUp script

Script info:
Type:Normal

--Script does its usual functions
--So basically this is at the end, when all the "StartUp" functions are done and the game has begun.


    local Module = require(Module) --Here we just locate the ModuleScript
    while true do 
        Module.TotalTime += 1
        wait(1)
    end

…but this could make the game a bit laggier and waste memory. And I am pretty sure there’s another way to do this that doesn’t require much memory.

So TL;DR: I want to see how long (in seconds) since a function has been done without using memory wasting loops.

Thanks for reading!

You can use this:

local startTime = os.time()
local timeLenght = os.time() - startTime

print(timeLenght)
2 Likes

You could store the time when the game first started using something like os.time(), then when the players wins or dies, subtract os.time() by the start time to get the seconds that have passed between starting and ending,

Basically what @AxeI_c mentioned before me

1 Like