Time
Global Time utility providing frame timing and server synchronization. Essential for frame-rate independent animations, physics, and time-based mechanics.
Use cases:
- Smooth, frame-rate independent movement (Time.deltaTime)
- Physics calculations (Time.fixedDeltaTime)
- Server time synchronization (Time.serverTime)
- Elapsed time tracking (Time.time)
Available on: Client and Server scripts
Properties
Time in seconds since the last frame. Use this to make movement and animations frame-rate independent. Multiply speeds by deltaTime to ensure consistent behavior regardless of frame rate.
IMPORTANT: Use in Update(), not FixedUpdate() (which already has fixed timing).
-- Smooth movement (frame-rate independent)
function self:Update()
local speed = 5 -- units per second
local movement = Vector3.forward * speed * Time.deltaTime
self.transform.position = self.transform.position + movement
end
-- Rotate object smoothly
local rotationSpeed = 90 -- degrees per second
function self:Update()
local rotation = self.transform.eulerAngles
rotation.y = rotation.y + (rotationSpeed * Time.deltaTime)
self.transform.eulerAngles = rotation
end
-- Count down timer
local countdown = 60
function self:Update()
countdown = countdown - Time.deltaTime
if countdown <= 0 then
print("Time's up!")
countdown = 60 -- Reset
end
end
The fixed time interval in seconds between physics updates. Typically 0.02 seconds (50 Hz). Consistent regardless of frame rate. Use in FixedUpdate() for physics calculations.
function self:FixedUpdate()
print("Fixed delta time: " .. Time.fixedDeltaTime)
-- Physics forces (already frame-rate independent in FixedUpdate)
local rb = self.gameObject:GetComponent(Rigidbody)
if rb then
rb:AddForce(Vector3.up * 10)
end
end
The current server time in seconds since the server started. Use for synchronizing events across multiple clients. Server-authoritative and consistent for all clients.
IMPORTANT: Use serverTime for multiplayer synchronization, not Time.time.
--!Type(Server)
-- Sync event across all clients at specific server time
local eventStartTime = Time.serverTime + 5 -- Start in 5 seconds
function self:ServerStart()
print("Server started at: " .. Time.serverTime)
-- Schedule synchronized event
Timer.After(5, function()
local event = Event.new("GameStart")
event:FireAllClients(eventStartTime)
end)
end
function self:ServerUpdate()
-- Check if it's time for an event
if Time.serverTime >= eventStartTime then
-- Start game logic
end
end
--!Type(ClientAndServer)
-- Track server time for cooldowns
local lastActionTime = 0
local cooldown = 3 -- 3 second cooldown
function self:ServerStart()
server.PlayerConnected:Connect(function(player)
-- Use server time for authoritative cooldowns
if Time.serverTime >= lastActionTime + cooldown then
lastActionTime = Time.serverTime
print("Action allowed")
else
print("Cooldown remaining: " .. (lastActionTime + cooldown - Time.serverTime))
end
end)
end
Time in seconds since the game started (scene loaded). Use for tracking elapsed time and creating time-based effects. Resets when the scene reloads.
-- Track total playtime
function self:Start()
print("Game started at time: " .. Time.time)
end
function self:Update()
print("Elapsed time: " .. Time.time .. " seconds")
end
-- Spawn enemies after certain time
function self:Update()
if Time.time >= 30 and not enemiesSpawned then
print("30 seconds elapsed - spawning enemies!")
enemiesSpawned = true
end
end
-- Periodic action using time
local nextActionTime = 0
function self:Update()
if Time.time >= nextActionTime then
print("Periodic action triggered")
nextActionTime = Time.time + 5 -- Every 5 seconds
end
end
Updated 19 days ago