> Agent-readable docs index: /llms.txt. Download /docs.zip to grep all markdown files locally.

---
title: _G
description: Built-in Lua global functions and utilities
icon: code-2
---

The `_G` namespace exposes Lua's built-in global functions along with engine-provided helpers for printing, time conversion, and class creation.

## assert

```lua
assert(expression: any[, text: string, ...]): any
```

> If the result of the first argument is false or `nil`, an error is thrown with the second argument as the message.

<Schema title="Parameters">
  <SchemaField name="expression" type="any" required>
    The expression to assert.
  </SchemaField>

  <SchemaField name="text" type="text">
    The error message to throw when assertion fails. This is only type-checked if the assertion fails.
  </SchemaField>

  <SchemaField name="..." type="any">
    Any arguments past the error message will be returned by a successful assert.
  </SchemaField>
</Schema>

<Schema title="Returns">
  <SchemaField name="value" type="any">
    The asserted value(s).
  </SchemaField>
</Schema>

## error

```lua
error(text: any[, level: number])
```

> Throws a Lua error and breaks out of the current call stack.

<Schema title="Parameters">
  <SchemaField name="text" type="string" required>
    The error message to throw.
  </SchemaField>

  <SchemaField name="level" type="number">
    The level to throw the error at.
  </SchemaField>
</Schema>

## getmetatable

```lua
getmetatable(object: any): any
```

> Returns the metatable of an object. This function obeys the metatable's \_\_metatable field, and will return that field if the metatable has it set.

<Schema title="Parameters">
  <SchemaField name="object" type="any" required>
    The value to return the metatable of.
  </SchemaField>
</Schema>

<Schema title="Returns">
  <SchemaField name="metatable" type="any">
    The metatable of the object.
  </SchemaField>
</Schema>

## ipairs

```lua
ipairs(tbl: table): function, table, number
```

> Returns an iterator function for a for loop, to return ordered key-value pairs from a table.

<Schema title="Parameters">
  <SchemaField name="tbl" type="table" required>
    The table to iterate over.
  </SchemaField>
</Schema>

<Schema title="Returns">
  <SchemaField name="iterator" type="function">
    The iterator function.
  </SchemaField>

  <SchemaField name="tbl" type="table">
    The table being iterated over.
  </SchemaField>

  <SchemaField name="index" type="number">
    The starting index.
  </SchemaField>
</Schema>

## print

```lua
print(text: string[, ...])
```

> Prints the text to the console.

<Schema title="Parameters">
  <SchemaField name="text" type="string" required>
    Text to print into the console.
  </SchemaField>

  <SchemaField name="..." type="any">
    Optional arguments to concatenate with text.
  </SchemaField>
</Schema>

## print\_error

```lua
print_error(text: string[, ...])
```

> Prints an error to the console and plays a sound.

<Schema title="Parameters">
  <SchemaField name="text" type="string" required>
    Text to print into the console.
  </SchemaField>

  <SchemaField name="..." type="any">
    Optional arguments to concatenate with text.
  </SchemaField>
</Schema>

## print\_chat

```lua
print_chat(text: string[, ...])
```

> Prints the text to the in-game chat.

<Schema title="Parameters">
  <SchemaField name="text" type="string" required>
    Text to print into the in-game chat.
  </SchemaField>

  <SchemaField name="..." type="any">
    Optional arguments to concatenate with text.
  </SchemaField>
</Schema>

## print\_raw

```lua
print_raw(text: string[, ...])
```

> Prints the text that can be changed in color by prepending it with `"\a"` followed by the color in the hexadecimal "RRGGBB" format. For example, `"\aFF0000Hi"` will print `"Hi"` in red.

<Schema title="Parameters">
  <SchemaField name="text" type="string" required>
    Text to print into the console.
  </SchemaField>

  <SchemaField name="..." type="any">
    Optional arguments to concatenate with text.
  </SchemaField>
</Schema>

## print\_dev

```lua
print_dev(text: string[, ...])
```

> Prints the text into the upper-left console panel.

<Schema title="Parameters">
  <SchemaField name="text" type="string" required>
    Text to print to into the upper-left console panel.
  </SchemaField>

  <SchemaField name="..." type="any">
    Optional arguments to concatenate with text.
  </SchemaField>
</Schema>

## tonumber

```lua
tonumber(value: any[, base: number]): number
```

> Returns the numeric representation of the value with the given base, or `nil` if the conversion failed.

<Schema title="Parameters">
  <SchemaField name="value" type="any" required>
    The value to convert. Can be a number or string.
  </SchemaField>

  <SchemaField name="base" type="number">
    Optional. The base used in the string. Can be any integer between 2 and 36, inclusive.
  </SchemaField>
</Schema>

<Schema title="Returns">
  <SchemaField name="number" type="number">
    The numeric representation of the value, or `nil` if conversion failed.
  </SchemaField>
</Schema>

## tostring

```lua
tostring(var: any): string
```

> Returns the string representation of the value.

<Schema title="Parameters">
  <SchemaField name="var" type="any" required>
    The object to be converted to a string.
  </SchemaField>
</Schema>

<Schema title="Returns">
  <SchemaField name="string" type="string">
    The string representation of the value.
  </SchemaField>
</Schema>

## type

```lua
type(var: any): string
```

> Returns the name of the object's type.

<Schema title="Parameters">
  <SchemaField name="var" type="any" required>
    The object to get the type of.
  </SchemaField>
</Schema>

<Schema title="Returns">
  <SchemaField name="string" type="string">
    The name of the object's type.
  </SchemaField>
</Schema>

## unpack

```lua
unpack(tbl: table, start_index: number, end_index: number): ...
```

> Generates a vararg from the values of a table.

<Schema title="Parameters">
  <SchemaField name="tbl" type="table" required>
    The table to generate the vararg from.
  </SchemaField>

  <SchemaField name="start_index" type="number">
    Which index to start from. Optional.
  </SchemaField>

  <SchemaField name="end_index" type="number">
    Which index to end at. Optional, even if you set start\_index.
  </SchemaField>
</Schema>

<Schema title="Returns">
  <SchemaField name="..." type="any">
    The values of the table as a vararg.
  </SchemaField>
</Schema>

## xpcall

```lua
xpcall(func: function, err_callback: function[, ...]): boolean, ...
```

> Attempts to call the first function. If the execution succeeds, this returns `true` followed by the returns of the function. If execution fails, this returns `false` and the second function is called with the error message.

<Schema title="Parameters">
  <SchemaField name="func" type="function" required>
    The function to call initially.
  </SchemaField>

  <SchemaField name="err_callback" type="function" required>
    The function to be called if execution of the first fails. The error message is passed as a string.
  </SchemaField>

  <SchemaField name="..." type="any">
    Arguments to pass to the initial function.
  </SchemaField>
</Schema>

<Schema title="Returns">
  <SchemaField name="boolean" type="boolean">
    `true` if execution succeeded, `false` otherwise.
  </SchemaField>

  <SchemaField name="..." type="any">
    The returns of the function on success.
  </SchemaField>
</Schema>

## to\_ticks

```lua
to_ticks(time: number): number
```

> Converts time (seconds) to ticks.

<Schema title="Parameters">
  <SchemaField name="time" type="number" required>
    The seconds to convert to ticks.
  </SchemaField>
</Schema>

<Schema title="Returns">
  <SchemaField name="number" type="number">
    The number of ticks.
  </SchemaField>
</Schema>

## to\_time

```lua
to_time(ticks: number): number
```

> Converts ticks to time (seconds).

<Schema title="Parameters">
  <SchemaField name="ticks" type="number" required>
    The number of ticks to convert to time.
  </SchemaField>
</Schema>

<Schema title="Returns">
  <SchemaField name="number" type="number">
    The time in seconds.
  </SchemaField>
</Schema>

## new\_class

```lua
new_class(): metatable
```

> Creates the new class.

<Info>
  This class system makes it easier to structure code in complex projects.
</Info>

<Schema title="Returns">
  <SchemaField name="metatable" type="metatable">
    The new class metatable.
  </SchemaField>
</Schema>

<Note>
  You can also create multiple structs and access one from another.
</Note>

<Expandable title="Example">
  ```lua Single struct lines=false
  local ctx = new_class()
      :struct 'struct_one' {
          variable1 = 'test',
          
          some_function = function(self, arg1)
              print(arg1)
              print(string.format('Hello World (%s)', self.variable1))
          end
      }
      
  ctx.struct_one:some_function('test')
  ```

  ```lua Multiple structs lines=false
  local ctx = new_class()
      :struct 'struct_one' {
          variable = 1,

          some_function = function(self)
              print(string.format(
                  'variable from struct_two: %d', 
                  self.struct_two.variable
              ))
          end
      }
      
      :struct 'struct_two' {
          variable = 2,
          
          some_function = function(self)
              print(string.format(
                  'variable from struct_one: %d', 
                  self.struct_one.variable
              ))
          end
      }

  ctx.struct_two:some_function()
  ctx.struct_one:some_function()

  print(ctx.struct_one.variable)
  ```
</Expandable>
