Introduction
Have you ever needed to add a countdown timer to a part? Or create a buff system with durations? Or maybe track cooldowns across multiple scripts?
Well say no more! Let me introduce AttributeTimer - a lightweight, efficient module that lets you attach timers to any Instance using the attribute system!
Why Use AttributeTimer?
Traditional timer approaches often involve storing references in tables, creating separate objects, or writing complex timer logic. AttributeTimer simplifies this by:
- Using attributes as the storage mechanism - making timer values accessible from practically anywhere
- It handles all the timing logic for you - no need to mess with RunService connections
- Being incredibly lightweight - just one module to require
How It Works
AttributeTimer attaches a timer value to any object as an attribute. This value automatically updates at your specified interval and can trigger callbacks when completed.
-- Basic usage
local AttributeTimer = require(path.to.AttributeTimer)
-- Create a 5-second cooldown on a part
AttributeTimer.Set(part, "Cooldown", 5)
-- The part now has a "Cooldown" attribute that counts down from 5 to 0
-- The nitty and gritty!
-- Create a timer with callbacks and options
AttributeTimer.Set(part, "Cooldown", 10, {
CountDown = true, -- Count down from 10 to 0 (default)
AutoUpdate = true, -- Auto-update the attribute (default)
Precision = 0.1, -- Update every 0.1 seconds (default)
DestroyOnComplete = false, -- Don't destroy the object when done (default)
-- Called when timer completes
Callback = function(object, attributeName)
print("Timer complete!")
end,
-- Called on each timer update
OnTick = function(object, attributeName, currentValue, elapsedTime)
-- Update UI or visual effects based on time
print("Time remaining:", currentValue)
end
})
Examples
Exploding Shenanigans
local AttributeTimer = require(path.to.AttributeTimer)
local function CreateBomb(Position)
-- Make the bomb here
end
local function CreateExplosion(object)
-- Create explosion here
end
local function PlantBomb(position)
local bomb = CreateBomb(position)
-- Create a 30-second timer until detonation
AttributeTimer.Set(bomb, "DetonationTimer", 30, {
OnTick = function(object, attributeName, currentValue)
-- Update the countdown display
CountdownLabel.Text = math.floor(currentValue)
-- Make it flash faster as time runs out
if currentValue < 5 then
object.Color = currentValue % 1 > 0.5 and Color3.new(1,0,0) or Color3.new(1,1,1)
end
end,
Callback = function(object)
-- Kaboom!
CreateExplosion(object)
object:Destroy()
end
})
Pausing/Resuming
-- Example of how to use pause and resume
local function PausableTimer(Part)
local TimerId = AttributeTimer.Set(Part, "PausableTimer", 30, {
OnTick = function(Object, AttributeName, CurrentValue)
print("Timer:", CurrentValue)
end,
Callback = function()
print("Timer complete!")
end
})
-- After 5 seconds, pause the timer
task.delay(5, function()
print("Pausing timer...")
AttributeTimer.Pause(Part, "PausableTimer")
-- Resume after 3 more seconds
task.delay(3, function()
print("Resuming timer...")
AttributeTimer.Resume(Part, "PausableTimer")
end)
end)
return TimerId
end
Documentation
-- Set a timer on an object
AttributeTimer.Set(object, attributeName, duration, options)
-- Options table can include:
-- {
-- CountDown = true, -- Count down from duration to 0 (default: true)
-- AutoUpdate = true, -- Update attribute value automatically (default: true)
-- Precision = 0.1, -- Update frequency in seconds (default: 0.1)
-- DestroyOnComplete = false, -- Destroy object when timer completes (default: false)
-- Callback = function(object, attributeName) end, -- Called when timer completes
-- OnTick = function(object, attributeName, currentValue, elapsedTime) end -- Called on each update
-- }
-- Control functions
AttributeTimer.Pause(object, attributeName) -- Pause a timer
AttributeTimer.Resume(object, attributeName) -- Resume a paused timer
AttributeTimer.Reset(object, attributeName) -- Reset a timer to its original duration
AttributeTimer.Clear(object, attributeName) -- Remove a timer
AttributeTimer.ClearAll() -- Remove all timers
AttributeTimer.GetRemaining(object, attributeName) -- Get remaining time
Get It Here!
Note: Please give me feedback! This is my first ever resource, anything is appreciated