AttributeTimer | Easiest Way to Add Timers to Any Object!

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! :clock3:

Why Use AttributeTimer?

Traditional timer approaches often involve storing references in tables, creating separate objects, or writing complex timer logic. AttributeTimer simplifies this by:

  1. Using attributes as the storage mechanism - making timer values accessible from practically anywhere
  2. It handles all the timing logic for you - no need to mess with RunService connections
  3. 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 :smiling_imp:

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! :grinning:

Note: Please give me feedback! This is my first ever resource, anything is appreciated :grin:

5 Likes

Looks cool, I’ll give it a try

1 Like

I may have already made this…

1 Like

It’s similar, however does yours rely on attributes?

It creates oop objects. Which is helpful for alot of things.

1 Like

Cool, however I mainly use attributes, and tend to stay away from OOP, this resource may be helpful to those who tend to veer away from OOP.