Usage Examples
Script Example: (NOT LOCALSCRIPT)
--[[
TaskScheduler Example
made by @knopka_01 :D
]]
-- Load the TaskScheduler module using its ID
local TaskScheduler = require(121440418698601)
-- Add a log entry saying the module has been loaded
TaskScheduler:AddLog("TaskScheduler Module Loaded", "require module")
-- Define the first task function (prints a message and waits for 1 second)
local function Task1()
print("Task 1 is running!")
wait(1)
return "Task 1 result" -- Returns a result after finishing
end
-- Define the second task function (prints a message and causes an error)
local function Task2()
print("Task 2 is running!")
error("Task 2 Error! Oh no!!!") -- Simulates an error to test retries
end
-- Define the third task function (prints a message and waits for 2 seconds)
local function Task3()
print("Task 3 is running!")
wait(2)
return "Task 3 result" -- Returns a result after finishing
end
-- Define the fourth task function (prints a message and waits for 3 seconds)
local function Task4()
print("Task 4 is running!")
wait(3)
return "Task 4 result" -- Returns a result after finishing
end
-- Add the first task to the TaskScheduler with a high priority and a timeout of 5 seconds
local task1 = TaskScheduler:AddTask("Task1", Task1, nil, TaskScheduler.PriorityLevels.High, 5)
-- Add the second task with normal priority, and it depends on Task1 to finish first
local task2 = TaskScheduler:AddTask("Task2", Task2, { "Task1" }, TaskScheduler.PriorityLevels.Normal, 5)
-- Add the third task with low priority, depending on Task2
local task3 = TaskScheduler:AddTask("Task3", Task3, { "Task2" }, TaskScheduler.PriorityLevels.Low, 5)
-- Add the fourth task with normal priority and a longer timeout of 10 seconds
local task4 = TaskScheduler:AddTask("Task4", Task4, nil, TaskScheduler.PriorityLevels.Normal, 10)
-- Group task1 and task2 under "NetworkTasks" for easy management
task1:SetCategory("NetworkTasks")
task2:SetCategory("NetworkTasks")
-- Group task3 and task4 under a namespace "Compute"
task3:SetNamespace("Compute")
task4:SetNamespace("Compute")
-- Set task2 to retry up to 3 times if it fails
task2.MaxRetries = 3
-- Use exponential retry strategy (increases wait time between retries)
task2.RetryStrategy = TaskScheduler.RetryStrategies.Exponential
-- Add a rule to throttle tasks in the "NetworkTasks" group to run every 2 seconds
TaskScheduler:AddThrottlingRule("NetworkThrottle", 2)
-- Assign the throttle rule to task1
task1.ThrottleKey = "NetworkThrottle"
-- Event that triggers when task1 starts
task1.OnStart = function(task)
print(task.Name .. " has started")
end
-- Event that triggers when task1 completes
task1.OnComplete = function(task)
print(task.Name .. " completed with result: " .. tostring(task.Result))
end
-- Event that triggers when task2 fails
task2.OnFail = function(task)
print(task.Name .. " failed with error: " .. tostring(task.Error))
end
-- Event that triggers when task2 retries after failing
task2.OnRetry = function(task)
print(task.Name .. " retrying, attempt " .. task.Retries)
end
-- Add task1 and task2 to a group called "ImportantTasks"
TaskScheduler:AddTaskToGroup("ImportantTasks", task1)
TaskScheduler:AddTaskToGroup("ImportantTasks", task2)
-- Add task3 and task4 to a group called "ComputeTasks"
TaskScheduler:AddTaskToGroup("ComputeTasks", task3)
TaskScheduler:AddTaskToGroup("ComputeTasks", task4)
-- Set a global error handler for any task that fails
TaskScheduler.GlobalErrorHandler = function(task)
print("Global error handler: Task " .. task.Name .. " failed.")
end
-- Run all tasks in the "ImportantTasks" group (task1 and task2)
TaskScheduler:RunGroup("ImportantTasks")
-- Wait for 2 seconds, then pause task3
task.wait(2)
TaskScheduler:PauseTask("Task3")
print("Task 3 paused")
-- Wait for 3 more seconds, then resume task3
task.wait(3)
TaskScheduler:ResumeTask("Task3")
print("Task 3 resumed")
-- Cancel task4, meaning it won't run
TaskScheduler:CancelTask("Task4")
print("Task 4 was cancelled")
-- Run all remaining tasks in the scheduler
TaskScheduler:RunAll()
-- Get all the logs and print them
local logs = TaskScheduler:GetLogs()
for i, log in ipairs(logs) do
print("Log " .. i .. ": " .. log.Message .. " (Time: " .. log.Time .. ")")
end
-- Example of how to cancel or pause tasks dynamically (for future use):
-- TaskScheduler:CancelTask("LoadPlayerData")
-- TaskScheduler:PauseAllTasks()
-- TaskScheduler:ResumeAllTasks()
LocalScript Example:
(Using a LocalScript will require manual installation of this module.)
Last updated