DirkScripts Logo

Login With FiveM

Login

πŸ“š Library
β€Ί
Modules
β€Ί

class

Class

ℹ️ Shared Module - This can be used by both clients and the server

lib.class

Create a new class definition with optional inheritance.

lua
local Animal = lib.class('Animal')

function Animal:constructor(name, sound)
    self.name = name
    self.private.sound = sound  -- private field
end

function Animal:speak()
    return self.name .. ' says ' .. self.private.sound
end

local dog = Animal:new('Rex', 'Woof')
print(dog:speak()) -- "Rex says Woof"

Signature

lua
local MyClass = lib.class(name, super)
ParameterTypeRequiredDescription
namestringyesClass name (used for type checking)
supertablenoParent class to inherit from

Inheritance

lua
local Dog = lib.class('Dog', Animal)

function Dog:constructor(name)
    self:super(name, 'Woof')  -- call parent constructor
end

function Dog:fetch(item)
    return self.name .. ' fetches the ' .. item
end

Type Checking

lua
lib.isClass(dog)            -- true
lib.instanceOf(dog, Animal) -- true
lib.instanceOf(dog, Dog)    -- true

Private Fields

Fields stored on self.private are only accessible from within the class methods. External access will error.

lua
print(dog.private.sound)  -- errors: cannot access private field

Copyright Β© 2026 DirkScripts.

Not affiliated with or endorsed by Rockstar North, Take-Two Interactive, or any other rights holders. FiveM is a copyright and registered trademark of Take-Two Interactive Software, Inc.
Our checkout system is provided by Tebex Limited, who manage payment processing, product delivery, and billing support. Prices shown in currencies other than GBP are approximate conversions updated daily. All purchases are processed in GBP, so the final amount charged may vary depending on your bank or payment provider’s exchange rate.