Scripting

Limits and safety

What scripts cannot do, the limits they run under, and scripts from other people.

The sandbox

Each script runs in its own Lua state and cannot see the others. Scripts cannot:

  • Open, read, write or delete files
  • Run programs or open links
  • Load compiled Lua code
  • Read environment variables or exit the game

The one thing a script can keep on disk is its own storage, and it cannot choose the file or reach another script’s.

So Lua’s io and debug libraries are not there, os only keeps clock, time, date and difftime, and dofile, loadfile and collectgarbage are removed. require does not exist either: put everything a script needs in its one file.

Limits

LimitWhat happens past it
64 MB of memory per scriptThe call that asked for more fails with a “not enough memory” error
20 million Lua instructions per callThe call stops with “ran too long”. One call is one handler run, one command, or the script’s first run when it loads. The timers a script fires on one tick share one call’s worth
256 timers per scripttimer.after and timer.every fail with an error
1 MB of storage per scriptstorage.set fails with an error
10 errors within 10 secondsThe script is stopped. .lua list shows why, and .lua load <name> starts it again

A call that hits a limit counts as an error, so a module whose handler hits it turns off. An endless loop cannot freeze the game.

Scripts from other people

Only run scripts you have read or trust. The sandbox keeps a script away from your files and PC, but inside the game a script can do what you can:

  • Send chat messages and game commands as you, with client.say
  • Turn modules on and off and change their settings
  • Hide chat messages and titles from you

client.command does not run every Lumen command. A script is refused the ones that reach your files, load other scripts or change your setup. The API reference lists them.

A short script is easy to check: look for client.say, client.command and modules.toggle, and make sure they only do what the script says it does.

Performance

Render handlers run every frame and tick handlers 20 times a second. Most scripts cost nothing you would notice, but a few habits keep it that way:

  • Do the work in tick and only draw in render.
  • Call world.entities() once per handler and keep the result, rather than calling it inside a loop.
  • Store values in local variables rather than globals.