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.gameObjectto access the GameObject this script is attached to - Use
self.transformto access the Transform component
Available on: Client and Server scripts
Methods
Returns
Returns
Called at fixed frame rate, CLIENT ONLY. Use for client-side physics.
Returns
Called after all ClientUpdate methods, CLIENT ONLY.
Returns
Returns
Returns
Returns
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
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
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
Called after all Update methods have completed. Use for camera following or operations that depend on other Updates.
Returns
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
hit
Returns
Called when this object stops colliding with another.
function self:OnCollisionExit(collision)
print("Stopped colliding with: " .. collision.gameObject.name)
end
Parameters
hit
Returns
Called every frame while colliding with another object.
function self:OnCollisionStay(collision)
-- Continuously handle collision
print("Still colliding with: " .. collision.gameObject.name)
end
Parameters
hit
Returns
Returns
Returns
Returns
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
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
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
Returns
Called at fixed frame rate, SERVER ONLY. Use for server-side physics and deterministic gameplay.
Returns
Called after all ServerUpdate methods, SERVER ONLY.
Returns
Returns
Returns
Returns
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
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
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
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
Updated 13 days ago