• Studio

  • Studio API

  • Bots

  • Web API

  • Designer Resources

  • Host Resources

  • Globals

    Self

    Inherits from:

    The self object represents your Lua script instance and provides access to Unity lifecycle methods. Inherits from LuaBehaviour, giving you access to self.gameObject and self.transform.

    Lifecycle Methods Available:

    • Shared: Start, Update, LateUpdate, FixedUpdate
    • Client-Only: ClientStart, ClientUpdate, ClientLateUpdate, ClientFixedUpdate
    • Server-Only: ServerStart, ServerUpdate, ServerLateUpdate, ServerFixedUpdate
    • Enable/Disable: OnEnable, OnDisable, OnDestroy (and Client/Server variants)
    • Physics: OnTriggerEnter, OnTriggerStay, OnTriggerExit, OnCollisionEnter, OnCollisionStay, OnCollisionExit

    IMPORTANT:

    • Client methods only run on client scripts
    • Server methods only run on server scripts
    • Shared methods run on both (or based on script type)
    • Use self.gameObject to access the GameObject this script is attached to
    • Use self.transform to access the Transform component

    Available on: Client and Server scripts

    Methods

    Awake

    ClientAndServer

    Returns

    void

    ClientAwake

    ClientAndServer

    Returns

    void

    ClientFixedUpdate

    ClientAndServer

    Called at fixed frame rate, CLIENT ONLY. Use for client-side physics.

    Returns

    void

    ClientLateUpdate

    ClientAndServer

    Called after all ClientUpdate methods, CLIENT ONLY.

    Returns

    void

    ClientOnDestroy

    ClientAndServer

    Returns

    void

    ClientOnDisable

    ClientAndServer

    Returns

    void

    ClientOnEnable

    ClientAndServer

    Returns

    void

    ClientStart

    ClientAndServer

    Called when the script is first loaded, CLIENT ONLY. Use for client-specific initialization like UI setup.

    --!Type(ClientAndServer)
    
    function self:ClientStart()
        print("Client started!")
        print("Local player: " .. client.localPlayer.name)
        
        -- Setup client-specific systems
        -- Initialize UI, input handlers, etc.
    end
    

    Returns

    void

    ClientUpdate

    ClientAndServer

    Called every frame, CLIENT ONLY. Use for UI updates, input handling, client-side animations.

    function self:ClientUpdate()
        -- Check for player input
        if Input.GetKeyDown(KeyCode.Space) then
            print("Space pressed!")
        end
        
        -- Update UI elements
        -- Handle camera movement
    end
    

    Returns

    void

    FixedUpdate

    ClientAndServer

    Called at a fixed frame rate (typically 50 times per second). Use for physics operations like applying forces. Frame-rate independent by default.

    function self:FixedUpdate()
        -- Apply physics force
        local rb = self.gameObject:GetComponent(Rigidbody)
        if rb then
            rb:AddForce(Vector3.up * 10)
        end
    end
    

    Returns

    void

    LateUpdate

    ClientAndServer

    Called after all Update methods have completed. Use for camera following or operations that depend on other Updates.

    Returns

    void

    OnCollisionEnter

    ClientAndServer

    Called when this object collides with another (not a trigger). Both GameObjects need colliders, and IsTrigger must be false on both.

    function self:OnCollisionEnter(collision)
        print("Collided with: " .. collision.gameObject.name)
        print("Impact force: " .. collision.relativeVelocity.magnitude)
    end
    

    Parameters

    Returns

    void

    OnCollisionExit

    ClientAndServer

    Called when this object stops colliding with another.

    function self:OnCollisionExit(collision)
        print("Stopped colliding with: " .. collision.gameObject.name)
    end
    

    Parameters

    Returns

    void

    OnCollisionStay

    ClientAndServer

    Called every frame while colliding with another object.

    function self:OnCollisionStay(collision)
        -- Continuously handle collision
        print("Still colliding with: " .. collision.gameObject.name)
    end
    

    Parameters

    Returns

    void

    OnDestroy

    ClientAndServer

    Returns

    void

    OnDisable

    ClientAndServer

    Returns

    void

    OnEnable

    ClientAndServer

    Returns

    void

    OnTriggerEnter

    ClientAndServer

    Called when a collider enters this object's trigger collider. Both GameObjects need a Collider, and one must have IsTrigger = true.

    function self:OnTriggerEnter(collider)
        print("Trigger entered by: " .. collider.gameObject.name)
        
        -- Check if it's a player
        local character = collider.gameObject:GetComponent(Character)
        if character then
            print("Player entered trigger zone!")
        end
    end
    

    Parameters

    collider

    Returns

    void

    OnTriggerExit

    ClientAndServer

    Called when a collider exits this object's trigger collider.

    function self:OnTriggerExit(collider)
        print("Trigger exited by: " .. collider.gameObject.name)
    end
    

    Parameters

    collider

    Returns

    void

    OnTriggerStay

    ClientAndServer

    Called every frame while a collider is inside this object's trigger collider.

    function self:OnTriggerStay(collider)
        -- Continuously check while inside trigger
        print("Object still in trigger: " .. collider.gameObject.name)
    end
    

    Parameters

    collider

    Returns

    void

    ServerAwake

    ClientAndServer

    Returns

    void

    ServerFixedUpdate

    ClientAndServer

    Called at fixed frame rate, SERVER ONLY. Use for server-side physics and deterministic gameplay.

    Returns

    void

    ServerLateUpdate

    ClientAndServer

    Called after all ServerUpdate methods, SERVER ONLY.

    Returns

    void

    ServerOnDestroy

    ClientAndServer

    Returns

    void

    ServerOnDisable

    ClientAndServer

    Returns

    void

    ServerOnEnable

    ClientAndServer

    Returns

    void

    ServerStart

    ClientAndServer

    Called when the script is first loaded, SERVER ONLY. Use for server-specific initialization like setting up game state.

    --!Type(ClientAndServer)
    
    function self:ServerStart()
        print("Server started!")
        
        -- Setup server-specific systems
        server.PlayerConnected:Connect(function(player)
            print(player.name .. " connected")
        end)
        
        -- Initialize game state
        Storage.SetValue("GameState", "waiting")
    end
    

    Returns

    void

    ServerUpdate

    ClientAndServer

    Called every frame, SERVER ONLY. Use for game logic, AI, server-authoritative gameplay.

    local gameTime = 0
    
    function self:ServerUpdate()
        -- Update game timer
        gameTime = gameTime + Time.deltaTime
        
        -- Server-side game logic
        -- AI updates
        -- Spawn management
    end
    

    Returns

    void

    Start

    ClientAndServer

    Called when the script is first loaded, before Update methods. Use for initialization logic that should run on both client and server. Runs based on script type (Client, Server, or ClientAndServer).

    function self:Start()
        print("Script initialized!")
        print("GameObject: " .. self.gameObject.name)
        print("Position: " .. tostring(self.transform.position))
    end
    

    Returns

    void

    Update

    ClientAndServer

    Called every frame. Use for frame-based logic and animations. Runs based on script type (Client, Server, or ClientAndServer). NOT frame-rate independent - use Time.deltaTime for smooth movement.

    function self:Update()
        -- Rotate object smoothly
        local rotation = self.transform.eulerAngles
        rotation.y = rotation.y + (90 * Time.deltaTime)
        self.transform.eulerAngles = rotation
    end
    

    Returns

    void

    Updated 13 days ago

    PocketWorlds Icon

    © 2025 Pocket Worlds. All rights reserved.