Resource
The Resource class is a static service for managing server resources. Resources are identified by unique names and can be started, stopped, restarted, and queried for information.
Error Handling Pattern​
This API uses the "Error or Nil" pattern.
- If a function succeeds, it returns
nilfor the error parameter - If a function fails, it returns a string error message
Static Service: Resource​
| Function | Description | Scope |
|---|---|---|
Resource.Start | Starts a specified resource | S |
Resource.Stop | Stops a specified resource | S |
Resource.Restart | Restarts a specified resource | S |
Resource.GetResources | Gets a list of all available resources | S |
Resource.GetResourceInfo | Gets detailed information about a specific resource | S |
Functions​
Resource.Start​
S Server Only
local err = Resource.Start(resourceName)
Parameters:
resourceName(string): The name of the resource to start S
Returns:
err:nilon success, or a string error message
Usage Examples:
-- Start a single resource
local err = Resource.Start("es_extended")
if err then
print("Failed to start resource: " .. err)
end
-- Start multiple resources
local resources = {"es_extended", "esx_basicneeds", "esx_identity"}
for _, resource in ipairs(resources) do
local err = Resource.Start(resource)
if err then
print("Failed to start " .. resource .. ": " .. err)
end
end
Resource.Stop​
S Server Only
local err = Resource.Stop(resourceName)
Parameters:
resourceName(string): The name of the resource to stop S
Returns:
err:nilon success, or a string error message
Usage Examples:
-- Stop a single resource
local err = Resource.Stop("es_extended")
if err then
print("Failed to stop resource: " .. err)
end
-- Stop multiple resources
local resources = {"es_extended", "esx_basicneeds", "esx_identity"}
for _, resource in ipairs(resources) do
local err = Resource.Stop(resource)
if err then
print("Failed to stop " .. resource .. ": " .. err)
end
end
Resource.Restart​
S Server Only
local err = Resource.Restart(resourceName)
Parameters:
resourceName(string): The name of the resource to restart S
Returns:
err:nilon success, or a string error message
Usage Examples:
-- Restart a single resource
local err = Resource.Restart("es_extended")
if err then
print("Failed to restart resource: " .. err)
end
-- Restart multiple resources
local resources = {"es_extended", "esx_basicneeds"}
for _, resource in ipairs(resources) do
local err = Resource.Restart(resource)
if err then
print("Failed to restart " .. resource .. ": " .. err)
end
end
Resource.GetResources​
S Shared (Client & Server)
local resources, err = Resource.GetResources()
Parameters:
- None
Returns:
resources(table): Array of resource names (or nil on failure)err:nilon success, or a string error message
Usage Examples:
-- Get all resources
local resources, err = Resource.GetResources()
if err then
return print("Failed to get resources: " .. err)
end
-- Print all resource names
for _, resourceName in ipairs(resources) do
print("Resource: " .. resourceName)
end
-- Filter resources by prefix
local esxResources = {}
for _, resourceName in ipairs(resources) do
if string.match(resourceName, "^esx_") then
table.insert(esxResources, resourceName)
end
end
print("Found " .. #esxResources .. " ESX resources")
Resource.GetResourceInfo​
S Shared (Client & Server)
local resourceInfo, err = Resource.GetResourceInfo(resourceName)
Parameters:
resourceName(string): The name of the resource to get information about S
Returns:
resourceInfo(table): Detailed resource information (or nil on failure)name(string): Resource nameauthor(string): Resource authorversion(string): Resource versiondescription(string): Resource descriptionstate(string): Current state ("started", "stopped", "unknown")dependencies(table): Array of dependency resource namesfiles(table): Array of file paths in the resource
err:nilon success, or a string error message
Usage Examples:
-- Get information about a specific resource
local resourceInfo, err = Resource.GetResourceInfo("es_extended")
if err then
return print("Failed to get resource info: " .. err)
end
-- Display resource information
print("Resource: " .. resourceInfo.name)
print("Author: " .. resourceInfo.author)
print("Version: " .. resourceInfo.version)
print("State: " .. resourceInfo.state)
print("Description: " .. resourceInfo.description)
-- Check dependencies
if resourceInfo.dependencies and #resourceInfo.dependencies > 0 then
print("Dependencies:")
for _, dependency in ipairs(resourceInfo.dependencies) do
print(" - " .. dependency)
end
end
-- Get info for all resources
local allResources, err = Resource.GetResources()
if not err then
for _, resourceName in ipairs(allResources) do
local info, getErr = Resource.GetResourceInfo(resourceName)
if not getErr then
print(resourceName .. " v" .. info.version .. " by " .. info.author .. " (" .. info.state .. ")")
end
end
end
Usage Examples (Complete Workflow)​
Server: Resource Management​
-- 1. Get all resources
local resources, err = Resource.GetResources()
if err then
return Logger.Error("Failed to get resources: " .. err)
end
-- 2. Find specific resources
local targetResources = {}
for _, resourceName in ipairs(resources) do
if string.match(resourceName, "^esx_") then
table.insert(targetResources, resourceName)
end
end
-- 3. Get detailed info before starting
for _, resourceName in ipairs(targetResources) do
local info, getErr = Resource.GetResourceInfo(resourceName)
if not getErr and info.state == "stopped" then
print("Starting " .. resourceName .. " v" .. info.version)
-- 4. Start the resource
local startErr = Resource.Start(resourceName)
if startErr then
Logger.Error("Failed to start " .. resourceName .. ": " .. startErr)
else
print("Successfully started " .. resourceName)
end
end
end
Client: Resource Information Display​
-- Display resource information to players
local function showResourceInfo(resourceName)
local info, err = Resource.GetResourceInfo(resourceName)
if err then
TriggerEvent('chat:addMessage', {
color = {255, 0, 0},
multiline = true,
args = {"System", "Error getting resource info: " .. err}
})
return
end
local message = string.format(
"Resource: %s\nAuthor: %s\nVersion: %s\nState: %s\n%s",
info.name,
info.author or "Unknown",
info.version or "Unknown",
info.state,
info.description or "No description available"
)
TriggerEvent('chat:addMessage', {
color = {0, 255, 0},
multiline = true,
args = {"Resource Info", message}
})
end
-- Example usage
showResourceInfo("es_extended")
Constants & Constraints​
| Constant | Value | Description |
|---|---|---|
Resource.MAX_NAME_LENGTH | 64 | Maximum characters for resource names |
Resource.RESTART_TIMEOUT | 30 | Maximum seconds to wait for restart |
Resource.START_TIMEOUT | 15 | Maximum seconds to wait for start |
Resource.STOP_TIMEOUT | 10 | Maximum seconds to wait for stop |
Resource.MAX_DEPENDENCIES | 50 | Maximum number of dependencies per resource |
Events​
Server Events​
| Event | Description | Parameters |
|---|---|---|
onResourceStart | Triggered when a resource starts successfully | resourceName (string) |
onResourceStop | Triggered when a resource stops | resourceName (string) |
onResourceRestart | Triggered when a resource restarts | resourceName (string) |
onResourceStartError | Triggered when a resource fails to start | resourceName (string), error (string) |
onResourceStopError | Triggered when a resource fails to stop | resourceName (string), error (string) |
Client Events​
| Event | Description | Parameters |
|---|---|---|
onClientResourceStart | Triggered when a resource loads on client | resourceName (string) |
onClientResourceStop | Triggered when a resource unloads on client | resourceName (string) |
Event Handlers​
onResourceStart​
S Server Only
AddEventHandler('onResourceStart', function(resourceName)
-- Resource started successfully
print("Resource started: " .. resourceName)
-- Example: Initialize dependent systems
if resourceName == "es_extended" then
InitializeESXSystems()
end
end)
Parameters:
resourceName(string): Name of the resource that started
onResourceStop​
S Server Only
AddEventHandler('onResourceStop', function(resourceName)
-- Resource stopped
print("Resource stopped: " .. resourceName)
-- Example: Cleanup dependent systems
if resourceName == "es_extended" then
CleanupESXSystems()
end
end)
Parameters:
resourceName(string): Name of the resource that stopped
onResourceRestart​
S Server Only
AddEventHandler('onResourceRestart', function(resourceName)
-- Resource restarted
print("Resource restarted: " .. resourceName)
-- Example: Re-initialize systems after restart
if resourceName == "es_extended" then
CleanupESXSystems()
Citizen.SetTimeout(1000, function()
InitializeESXSystems()
end)
end
end)
Parameters:
resourceName(string): Name of the resource that restarted
onResourceStartError​
S Server Only
AddEventHandler('onResourceStartError', function(resourceName, error)
-- Resource failed to start
print("Resource failed to start: " .. resourceName .. " - " .. error)
-- Example: Log to external monitoring
LogResourceError(resourceName, "START", error)
-- Example: Attempt recovery
if resourceName == "critical_resource" then
Citizen.SetTimeout(5000, function()
Resource.Start(resourceName)
end)
end
end)
Parameters:
resourceName(string): Name of the resource that failed to starterror(string): Error message describing the failure
onResourceStopError​
S Server Only
AddEventHandler('onResourceStopError', function(resourceName, error)
-- Resource failed to stop
print("Resource failed to stop: " .. resourceName .. " - " .. error)
-- Example: Force stop after timeout
Citizen.SetTimeout(10000, function()
Resource.Stop(resourceName)
end)
end)
Parameters:
resourceName(string): Name of the resource that failed to stoperror(string): Error message describing the failure
onClientResourceStart​
C Client Only
AddEventHandler('onClientResourceStart', function(resourceName)
-- Resource loaded on client
print("Client resource started: " .. resourceName)
-- Example: Initialize UI components
if resourceName == "custom_hud" then
InitializeHUD()
end
-- Example: Load client-side assets
if resourceName == "vehicle_mods" then
LoadVehicleTextures()
end
end)
Parameters:
resourceName(string): Name of the resource that loaded on client
onClientResourceStop​
C Client Only
AddEventHandler('onClientResourceStop', function(resourceName)
-- Resource unloaded on client
print("Client resource stopped: " .. resourceName)
-- Example: Cleanup UI components
if resourceName == "custom_hud" then
CleanupHUD()
end
-- Example: Unload client-side assets
if resourceName == "vehicle_mods" then
UnloadVehicleTextures()
end
end)
Parameters:
resourceName(string): Name of the resource that unloaded on client
Production-Ready Patterns​
Safe Resource Management​
-- Production-safe resource startup with validation
local function SafeStartResource(resourceName)
-- Validate resource name
if not resourceName or type(resourceName) ~= "string" then
return "Invalid resource name"
end
if #resourceName > Resource.MAX_NAME_LENGTH then
return "Resource name too long"
end
-- Check if resource exists
local resources, err = Resource.GetResources()
if err then
return "Failed to get resource list: " .. err
end
local resourceExists = false
for _, name in ipairs(resources) do
if name == resourceName then
resourceExists = true
break
end
end
if not resourceExists then
return "Resource not found: " .. resourceName
end
-- Check current state
local info, err = Resource.GetResourceInfo(resourceName)
if err then
return "Failed to get resource info: " .. err
end
if info.state == "started" then
return nil -- Already started, consider this success
end
-- Start with timeout protection
local startTime = GetGameTimer()
local err = Resource.Start(resourceName)
if err then
return "Failed to start resource: " .. err
end
-- Wait for start confirmation with timeout
local timeout = Resource.START_TIMEOUT * 1000 -- Convert to milliseconds
while (GetGameTimer() - startTime) < timeout do
local currentInfo, checkErr = Resource.GetResourceInfo(resourceName)
if not checkErr and currentInfo.state == "started" then
return nil -- Success
end
Citizen.Wait(100)
end
return "Resource start timeout"
end
-- Usage
local err = SafeStartResource("es_extended")
if err then
Logger.Error("Resource start failed: " .. err)
-- Implement fallback logic
end
Dependency Management​
-- Production dependency resolution
local function StartWithDependencies(resourceName)
local info, err = Resource.GetResourceInfo(resourceName)
if err then
return "Failed to get resource info: " .. err
end
-- Start dependencies first
if info.dependencies and #info.dependencies > 0 then
for _, dependency in ipairs(info.dependencies) do
local depInfo, depErr = Resource.GetResourceInfo(dependency)
if not depErr and depInfo.state ~= "started" then
local startErr = SafeStartResource(dependency)
if startErr then
return "Failed to start dependency " .. dependency .. ": " .. startErr
end
end
end
end
-- Start the resource
return SafeStartResource(resourceName)
end
Batch Operations​
-- Production-safe batch operations
local function BatchOperation(resources, operation)
local results = {}
local successCount = 0
for _, resourceName in ipairs(resources) do
local err
if operation == "start" then
err = SafeStartResource(resourceName)
elseif operation == "stop" then
err = Resource.Stop(resourceName)
elseif operation == "restart" then
err = Resource.Restart(resourceName)
else
err = "Unknown operation: " .. operation
end
results[resourceName] = err
if not err then
successCount = successCount + 1
end
-- Small delay between operations to prevent overwhelming
Citizen.Wait(100)
end
return results, successCount
end
Error Codes​
ERR_NOT_FOUND: Resource does not existERR_ALREADY_STARTED: Resource is already runningERR_ALREADY_STOPPED: Resource is already stoppedERR_DEPENDENCY_MISSING: Required dependency is not availableERR_PERMISSION_DENIED: Insufficient permissions to manage resourceERR_START_TIMEOUT: Resource failed to start within timeout periodERR_STOP_TIMEOUT: Resource failed to stop within timeout periodERR_INVALID_NAME: Resource name contains invalid charactersERR_NAME_TOO_LONG: Resource name exceeds maximum lengthERR_DEPENDENCY_CYCLE: Circular dependency detectedERR_SYSTEM_RESOURCE: Cannot modify system resourcesERR_RESOURCE_LOCKED: Resource is locked by another operation