TruWorlds

Globals

TruScript features a set of global functions and properties that can be accessed without a module prefix.

Properties

this Instance

The instance this script is attached to.

game DataModel

The root DataModel object of the instance tree. Game-wide services such as the world, players, and game systems are accessed through it.

world World

The world containing all parts, characters, and physical objects. This is a shorthand for game.World.

Functions

print(...values)

Prints the given values to the output log.

printf(format: string, ...args)

Prints a string replacing placeholders ("My name is {0} and I like {1}!") with the remaining arguments. This is a shorthand for print(string.format(format, ...args))

warn(...values)

Prints the given values to the output log as a warning.

error(...values)

Raises an error with the given values as the error message, stopping script execution and showing the call stack trace.

wait() Returns number

Pauses the script for the shortest possible time. Returns the actual time waited, in seconds.

wait(seconds: number) Returns number

Pauses the script and resumes when at least the given number of seconds has passed. The script scheduler will try to resume as soon as the target time elapsed, but the actual time waited may be longer than the specified time. Returns the actual time waited, in seconds.

spawn(fn: function, ...args) Returns coroutine

Runs the given function concurrently as a new coroutine, passing any additional arguments to it. Returns a reference to the newly-created coroutine.

range(end: number) Returns enumerator

Returns an enumerator for use in a for loop that counts from 0 up to (but not including) end, in steps of 1. For example, for i in range(3): visits 0, 1, and 2.

range(start: number, end: number) Returns enumerator

Returns an enumerator for use in a for loop that counts from start up to (but not including) end, in steps of 1.

range(start: number, end: number, step: number) Returns enumerator

Returns an enumerator for use in a for loop that counts from start toward end (exclusive) in increments of step. A negative step counts downward. The step's sign must match the range's direction, and a step of zero is an error.

time() Returns number

Returns the current time as the number of seconds since the Unix epoch (January 1, 1970 UTC), with fractional millisecond precision.

tonumber(value: any) Returns number

Converts the value to a number. Returns NaN when the value cannot be parsed as a number.

tobool(value: any) Returns boolean

Converts the value to a boolean using truthiness rules. As a special case, the string "false" converts to false, so this function is useful for processing data from remote sources like JSON.

tostring(value: any) Returns string

Converts the value to its string representation.

swap(a: string, b: string)

Swaps the values of two variables, given their names as strings. For example swap("x", "y") exchanges the values of x and y. Both names must be valid and in-scope.