107 online 3 members  + Join RP Base  +
FreeScript

switch

by PocusSoftware  ·  version 2026.11.04  ·  updated 3 days ago

No reviews yet

Downloads
0
Reviews
0
Size
5 KB
Code check
Clean
Licence
LGPL-3.0
Released
Nov 2026

A chainable switch for FiveM Lua

switch
switch
A chainable switch for FiveM Lua.
Install
Copy switch.lua into your resource and load it in your fxmanifest.lua:

lua54 'yes'
shared_script 'switch.lua'

lua54 'yes' is required.
Usage
switch(value) starts a chain. :case(...) takes one or more values followed by a handler, and the first matching case wins. :default(fn) runs if nothing matched. :get() returns whatever the handler returned.

local rate = switch(GetVehicleClass(veh))
    :case(8, function() return 0.6 end)
    :default(function() return 1.0 end)
    :get()

Multiple cases
Cases are checked top to bottom and the first match wins.

local label = switch(GetVehicleClass(veh))
    :case(8, function() return 'motorcycle' end)
    :case(13, function() return 'bicycle' end)
    :case(14, function() return 'boat' end)
    :case(15, function() return 'helicopter' end)
    :case(16, function() return 'plane' end)
    :default(function() return 'car' end)
    :get()

Multiple values per case
local ok, reason = switch(state)
    :case('locked', 'broken', function() return false, 'unavailable' end)
    :default(function() return true end)
    :get()

Multiple return values
:get() returns every value the handler returned.

local function getWheels(class)
    local _, _, wheels  = switch(class)
        :case(8, function() return 'bike', 0.6, 2 end)
        :default(function() return 'car', 1.0, 4 end)
        :get()
    return wheels
end

Wrap the call in parentheses to keep only the first value:

local function getLabel(class)
    return (switch(class)
        :case(8, function() return 'bike', 0.6, 2 end)
        :default(function() return 'car', 1.0, 4 end)
        :get())
end

Passing arguments to handlers
Inline handlers can use surrounding locals directly. Any extra arguments given to switch are passed to the handler, which is useful when the handlers are named functions defined elsewhere:

local function policeBuy(src, item, amount, price) ... end
local function deny(src) ... end

switch(job, source, item, amount, price)
    :case('police', policeBuy)
    :default(deny)

License
LGPL-3.0-or-later, see LICENSE.