Your First Character

Characters is another name for a component, which you can attach to an instance by giving it a tag. It is essentially the same as creating a class and binding it to a CollectionService tag.

YOU WILL LEARN HOW TO

  • Write your own Character

  • Use your Character's lifecycles

  • Set your Character's priority


What is a Character?

A Character is synonymous with a component or an instance-attached class. Instead of creating a metatable and then defining an instance for it, Characters simplify this process by automatically assigning themselves to a given tag. You can then treat it as if it was a regular luau class by adding functions to the Character.


Writing a Character

Every book needs its own characters! Here is how you might write your own Character for your workflow:

ServerScriptService.server_book.characters.example.luau
-- "init" is a lifecycle hook that always runs first!
local function init(self) -- "self" passes an instance attached to the tag.
    print("Character is attached to " .. self.instance:GetFullName())
end

-- "destroy" is a lifecycle hook that runs on the instance being removed.
local function destroy(self)
    print("Character " .. self.instance:GetFullName() .. " has been destroyed!")
end

return {
    tag = "Example_Tag", -- This Character now binds to any instance with this tag.
    init = init,
    destroy = destroy,
}

If you did everything correctly, this should now print the full names of any instance that has the Example_Tag tag.


Using a Character


Summary

  • Characters are components that provide functions to tagged instances.

  • Lifecycle hooks are automatically called on runtime, allowing Characters to run automatically and independently.

Last updated