AdjustableTimer
The AdjustableTimer class provides a flexible timer system that allows for pausing, resuming, adjusting time scales, and tracking elapsed time.
It is designed to be serialized and unserialized for replication and storage across systems. Use in conjunction with the AdjustableTimerManager
for
easy replication of timers from server to clients.
Example Usage
local timer = AdjustableTimer.new()
timer:Resume() -- Start the timer
task.wait(5) -- Simulate some elapsed time
print(timer:GetElapsedTime())
Functions
new
Creates a new AdjustableTimer instance. Takes an optional configuration table.
TimeScale
: The initial time scale for the timer. Defaults to 1.StartTime
: The initial start time of the timer. Defaults to the current server time (via workspace:GetServerTimeNow()).
local timer = AdjustableTimer.new()
timer:Resume() -- Start the timer
Starting the Timer
Timers are initialized in a paused state by default. You need to call :Resume()
to start the timer.
Unserialize
Unserializes a table into an AdjustableTimer instance.
local timer = AdjustableTimer.Unserialize(serializedData)
Serialize
AdjustableTimer:
Serialize
(
) →
SerializedAdjustableTimer
Serializes the timer into a saveable/network-safe table.
local serializedData = timer:Serialize()
GetElapsedTime
AdjustableTimer:
GetElapsedTime
(
) →
number
--
The elapsed time in seconds.
Gets the elapsed time that the timer has been running unpaused.
print(timer:GetElapsedTime())
Pause
Pauses the timer. Returns the timer for chaining.
timer:Pause()
Resume
Resumes the timer. Returns the timer for chaining.
timer:Resume()
IsPaused
AdjustableTimer:
IsPaused
(
) →
boolean
Returns whether or not the timer is paused.
print(timer:IsPaused())
SetTimeScale
AdjustableTimer:
SetTimeScale
(
newRate:
number
,
--
The new rate at with time will progress.
_newRateChangeTime:
number?
) →
(
)
Changes how fast the timer progresses every unpaused second.
2
means the timer will progress twice as fast, 0.5
means it will progress half as fast.
timer:SetTimeScale(2)
GetTimeScale
AdjustableTimer:
GetTimeScale
(
) →
number
--
The current progress rate.
Gets the current time scale of the timer. Represents how fast the timer progresses every unpaused second.
2
means the timer will progress twice as fast, 0.5
means it will progress half as fast.
AddElapsedTime
AdjustableTimer:
AddElapsedTime
(
seconds:
number
,
--
The number of seconds to add.
useTimeScale:
boolean?
--
Whether to apply the current time scale to the given seconds (default is false).
) →
(
)
Adds progress to the timer in raw seconds.
local time = AdjustableTimer.new():Resume()
print(timer:GetElapsedTime()) -- 0
timer:AddElapsedTime(10)
print(timer:GetElapsedTime()) -- 10
timer:SetTimeScale(2)
timer:AddElapsedTime(5, true)
print(timer:GetElapsedTime()) -- 20
SetElapsedTime
AdjustableTimer:
SetElapsedTime
(
seconds:
number
--
The elapsed time to set.
) →
(
)
Sets the elapsed time of the timer in raw seconds.
timer:SetElapsedTime(30)
SetStartTime
AdjustableTimer:
SetStartTime
(
timestamp:
number
) →
(
)
Sets the time at which the timer started. This will affect the elapsed time calculation.
The timestamp should be relative to workspace:GetServerTimeNow()
.
Does not account for time scale or paused state.
timer:SetStartTime(workspace:GetServerTimeNow() - 60) -- Set start time to 60 seconds ago