Return to Main Page

Creating & Running

Page white text.png Description:This is an introduction to the lua language in general and is a recommended read if you are a beginner.
link=User:Dim1xs Author:Dim1xs
Calendar.png Created:September 28th 2023

Introdution

A lua file is called a script, to create and edit these scripts you need a Lua Editor such as notepad.

You can use any text editor, but to make your life easier, we recommend that you use Quick Edit.

Creating the script


For our first script, we're not going to do anything too complex. Just send some message into game console.

Type the following code into your chosen editor :
print( "Hello World!")
               

Saving the script


You're now ready to create the actual Lua script file. To find your lua folder follow the following path - it will look something like this:

srceng/hl2/custom/hl2gmed/lua/*
               
And you must save it with lua format (.lua)

Running the script

Running file script

To run any of your scripts you need to be playing a map.
Now, open the console and type the following :
lua_dofile scriptname.lua
               
Make sure, using lua_dofile command will run script from NewerServer.pngSERVER side.

That also means using lua_dofile_cl command will run script from NewerClient.pngCLIENT side.

Send it. If you did everything correctly up to this point you should see a message in the console :
Hello World!
               
The script has done exactly what you told it to.

Running script line


By using lua_dostring command, line from argument, will be executed from Lua NewerServer.pngSERVER side.

So logically using lua_dostring_cl command, same will be done from NewerClient.pngCLIENT side.

Look at the example:
lua_dostring print("Hello World")
               
After sending that command let's look at the output:
Hello World
               

Autorun


You can also use 'lua/autorun/' directory, for make script run every map start.

Autorun Structure

  • NewerShared.png 'lua/autorun/ - Shared
  • NewerClient.png 'lua/autorun/client/ - Client
  • NewerServer.png 'lua/autorun/server/ - Server
Simply just put lua script into one of those folders listed above, and it gonna be exectued at the map start.


Thank you for reading this tutorial, see ya later!

JOIN HL2GMED DISCORD SERVER!