<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.multitheftauto.com/wiki/Scripting_Introduction_temp?action=history&amp;feed=atom</id>
	<title>Scripting Introduction temp - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.multitheftauto.com/wiki/Scripting_Introduction_temp?action=history&amp;feed=atom"/>
	<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=Scripting_Introduction_temp&amp;action=history"/>
	<updated>2026-06-24T18:45:23Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://wiki.multitheftauto.com/index.php?title=Scripting_Introduction_temp&amp;diff=82832&amp;oldid=prev</id>
		<title>Tails: Created page with &quot;==MTA Scripting: From Zero to Your First Script==  Many beginners think scripting requires pages of complex code before anything happens. In Multi Theft Auto (MTA), a fully working script can be as short as a single line.   This tutorial will show you the bare basics of how scripts work, how to navigate the MTA Wiki to find functions, and how to expand your script progressively into interactive commands and events.  ===What is a &quot;Resource&quot;?=== In MTA, everything you run...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.multitheftauto.com/index.php?title=Scripting_Introduction_temp&amp;diff=82832&amp;oldid=prev"/>
		<updated>2026-06-23T20:31:34Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;==MTA Scripting: From Zero to Your First Script==  Many beginners think scripting requires pages of complex code before anything happens. In Multi Theft Auto (MTA), a fully working script can be as short as a single line.   This tutorial will show you the bare basics of how scripts work, how to navigate the MTA Wiki to find functions, and how to expand your script progressively into interactive commands and events.  ===What is a &amp;quot;Resource&amp;quot;?=== In MTA, everything you run...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==MTA Scripting: From Zero to Your First Script==&lt;br /&gt;
&lt;br /&gt;
Many beginners think scripting requires pages of complex code before anything happens. In Multi Theft Auto (MTA), a fully working script can be as short as a single line. &lt;br /&gt;
&lt;br /&gt;
This tutorial will show you the bare basics of how scripts work, how to navigate the MTA Wiki to find functions, and how to expand your script progressively into interactive commands and events.&lt;br /&gt;
&lt;br /&gt;
===What is a &amp;quot;Resource&amp;quot;?===&lt;br /&gt;
In MTA, everything you run on a server (gamemodes, maps, custom scripts) is called a '''resource'''. &lt;br /&gt;
A resource is simply a folder inside your server directory that contains:&lt;br /&gt;
# One configuration file named '''meta.xml''' (which tells the server what files to load).&lt;br /&gt;
# One or more script files written in the '''Lua''' programming language.&lt;br /&gt;
&lt;br /&gt;
====Server-side vs. Client-side====&lt;br /&gt;
MTA splits scripts into two distinct environments:&lt;br /&gt;
* '''Server-side (Server):''' Scripts that run on the host server. They manage things that affect everyone, like the game world, weather, global variables, and player accounts.&lt;br /&gt;
* '''Client-side (Client):''' Scripts that run on the individual player's computer. They handle things the player sees directly on their screen, like custom user interfaces (GUIs) and visual effects.&lt;br /&gt;
&lt;br /&gt;
In this tutorial, we will start with '''Server-side''' scripting, and then show you how to transition to the client.&lt;br /&gt;
&lt;br /&gt;
---&lt;br /&gt;
&lt;br /&gt;
==Step 1: The One-Line Script==&lt;br /&gt;
Let us create the absolute simplest working script possible: a script that sets the game world's weather.&lt;br /&gt;
&lt;br /&gt;
Navigate to your server's resources directory:&lt;br /&gt;
  server/mods/deathmatch/resources/&lt;br /&gt;
&lt;br /&gt;
Create a new folder here and name it: `my_first_script`&lt;br /&gt;
&lt;br /&gt;
====1. Create your meta.xml====&lt;br /&gt;
Inside your `my_first_script` folder, create a plain text file, name it `meta.xml`, and type or paste the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;meta&amp;gt;&lt;br /&gt;
     &amp;lt;info author=&amp;quot;YourName&amp;quot; type=&amp;quot;script&amp;quot; name=&amp;quot;My First Script&amp;quot; description=&amp;quot;Simple weather script&amp;quot; /&amp;gt;&lt;br /&gt;
     &amp;lt;script src=&amp;quot;script.lua&amp;quot; type=&amp;quot;server&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/meta&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''Reminder: The `&amp;lt;script /&amp;gt;` tag tells the server engine to locate a file named `script.lua` and run it in the server environment (`type=&amp;quot;server&amp;quot;`).''&lt;br /&gt;
&lt;br /&gt;
====2. Write your script.lua====&lt;br /&gt;
In the same folder, create a text file named `script.lua`. Open it in your code editor and '''type''' this exact line of code yourself:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
setWeather(8)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let us look at what you just wrote. You called a function named [[setWeather]] and passed it an argument of `8`. The number `8` is an identifier that tells the MTA engine to change the sky to a raining thunderstorm. &lt;br /&gt;
&lt;br /&gt;
====3. Run the script====&lt;br /&gt;
# Start your server executable (`MTA Server.exe`).&lt;br /&gt;
# In the server console window, type: `start my_first_script` and press Enter.&lt;br /&gt;
# Connect to your server in-game. When you spawn, the weather will instantly become rainy.&lt;br /&gt;
&lt;br /&gt;
---&lt;br /&gt;
&lt;br /&gt;
==Understanding the MTA Wiki==&lt;br /&gt;
Now, you might be wondering: how did we know that [[setWeather]] exists, or that putting an `8` inside the parentheses makes it rain? &lt;br /&gt;
&lt;br /&gt;
There is an easy solution for that: The MTA Wiki. You do not need to memorize code; you just need to know how to read the documentation.&lt;br /&gt;
&lt;br /&gt;
If you search for [[setWeather]] on the wiki, you will see a syntax block that looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
bool setWeather ( int weatherID )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To be able to understand how functions work in MTA, we need to learn how to read this syntax on the wiki. &lt;br /&gt;
&lt;br /&gt;
====How to read this syntax:====&lt;br /&gt;
* '''bool''' (Boolean): The first word tells you what the function ''returns'' after it finishes. A boolean means it gives back either `true` (success) or `false` (failure).&lt;br /&gt;
* '''setWeather''': The exact name of the function you must type.&lt;br /&gt;
* '''int weatherID''' (Integer): The information you must provide inside the parentheses. An `int` expects a whole number (like `0`, `1`, or `8`). The documentation page lists what each number means (e.g., `8` is a rainstorm).&lt;br /&gt;
&lt;br /&gt;
Now let us look at another essential function, [[outputChatBox]]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
bool outputChatBox ( string text [, element visibleTo=root, int r=255, int g=255, int b=255, bool colorCoded=false ] )&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====How to read optional parameters:====&lt;br /&gt;
* '''string text''': The first argument must be a string (regular text wrapped in quotation marks, like `&amp;quot;Hello world&amp;quot;`).&lt;br /&gt;
* '''Square Brackets `[ , ... ]`''': Anything inside square brackets is '''optional'''. You do not have to type it.&lt;br /&gt;
* '''Default Values (`=`)''': If you skip an optional argument, the engine uses the default value shown. For example, `visibleTo=root` means if you don't specify a target player, the message defaults to `root` (meaning it sends to everyone). `r=255, g=255, b=255` defaults the text color to white.&lt;br /&gt;
&lt;br /&gt;
---&lt;br /&gt;
&lt;br /&gt;
==Step 2: Making Things Interactive (Events)==&lt;br /&gt;
Our one-line script runs immediately when the resource starts, but games are interactive. You usually want code to trigger only when a specific action happens in the world—like a player entering a vehicle or taking damage. We handle this using [[Event|Events]].&lt;br /&gt;
&lt;br /&gt;
Let us replace our script with something more interesting: greeting players personally the moment they join the server.&lt;br /&gt;
&lt;br /&gt;
Open your `script.lua`, delete the old code, and write this complete updated code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- Define a function containing the actions we want to happen&lt;br /&gt;
function welcomeNewPlayer()&lt;br /&gt;
    -- 'source' is a built-in variable representing the player who just joined&lt;br /&gt;
    outputChatBox(&amp;quot;Welcome to the server!&amp;quot;, source)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Connect our function to the built-in player join event&lt;br /&gt;
addEventHandler(&amp;quot;onPlayerJoin&amp;quot;, root, welcomeNewPlayer)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====How this flows:====&lt;br /&gt;
# '''function welcomeNewPlayer()''': We group our code inside a named block called a function. This code sits silently and does nothing until it is called.&lt;br /&gt;
# '''[[addEventHandler]]''': We tell the server to listen for a specific trigger.&lt;br /&gt;
# '''[[onPlayerJoin]]''': The built-in MTA event that triggers whenever any player connects.&lt;br /&gt;
# '''root''': This tells the event listener where to listen. Using the predefined global variable `root` means &amp;quot;listen for anyone connecting to the entire server.&amp;quot;&lt;br /&gt;
# '''welcomeNewPlayer''': The name of the function we want to fire when the event happens.&lt;br /&gt;
# '''[[source]]''': Inside an event function, `source` is a hidden variable created by MTA. You can refer to the wiki page for [[source]] to learn more. For `onPlayerJoin`, `source` always refers to the specific player element who just connected.&lt;br /&gt;
&lt;br /&gt;
---&lt;br /&gt;
&lt;br /&gt;
==Step 3: Giving Control to Players (Commands)==&lt;br /&gt;
Next, let us allow players to trigger code whenever they type a command in the chat box, such as `/rain`.&lt;br /&gt;
&lt;br /&gt;
To make a command, we follow a flow very similar to events: we write a function, and then we link that function to a command word using [[addCommandHandler]].&lt;br /&gt;
&lt;br /&gt;
Replace the code in your `script.lua` with this basic command setup:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- 1. Write the function we want the command to trigger&lt;br /&gt;
function makeSkyRain()&lt;br /&gt;
    setWeather(8)&lt;br /&gt;
    outputChatBox(&amp;quot;The sky is now dark and raining!&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- 2. Hook the command word &amp;quot;/rain&amp;quot; to our function&lt;br /&gt;
addCommandHandler(&amp;quot;rain&amp;quot;, makeSkyRain)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you restart your resource and type `/rain` in the game chat, the weather will instantly start storming.&lt;br /&gt;
&lt;br /&gt;
===Adding Arguments to Commands===&lt;br /&gt;
What if we want the player to choose the weather ID themselves? Like typing `/setweather 8`. &lt;br /&gt;
&lt;br /&gt;
When a player uses a server-side command handler, MTA automatically passes three variables into your function:&lt;br /&gt;
# The '''player''' who typed the command.&lt;br /&gt;
# The '''name''' of the command itself.&lt;br /&gt;
# Whatever '''text''' the player typed after the space.&lt;br /&gt;
&lt;br /&gt;
Let us update `script.lua` to accept a weather ID argument. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- The function receives: the player element, the command name, and the extra text&lt;br /&gt;
function customWeatherCommand(thePlayer, commandName, weatherIDText)&lt;br /&gt;
&lt;br /&gt;
    -- Convert the typed text into a real number&lt;br /&gt;
    local weatherNumber = tonumber(weatherIDText)&lt;br /&gt;
&lt;br /&gt;
    -- Validation check: If the player didn't type a number, stop right here&lt;br /&gt;
    if not weatherNumber then&lt;br /&gt;
        outputChatBox(&amp;quot;Syntax: /setweather [ID number]&amp;quot;, thePlayer)&lt;br /&gt;
        return -- 'return' instantly stops the function from continuing&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    -- If the check passed, change the weather to the typed number&lt;br /&gt;
    setWeather(weatherNumber)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
addCommandHandler(&amp;quot;setweather&amp;quot;, customWeatherCommand)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Server-Side vs. Client-Side Commands===&lt;br /&gt;
Note that we have configured `script.lua` to be on the server side in our `meta.xml`. This means the command will run on the server side and change the weather for '''everyone''' on the server! &lt;br /&gt;
&lt;br /&gt;
Depending on your server, you might not want players changing the weather for everybody. We could fix this in two ways:&lt;br /&gt;
# Make it so only administrators are able to use the command.&lt;br /&gt;
# OR, write the command on the '''client side''' instead. &lt;br /&gt;
&lt;br /&gt;
Let us look at how to implement the second option. A client-side script will only change the weather for the player who typed the command, leaving everyone else's weather untouched.&lt;br /&gt;
&lt;br /&gt;
====Modifying the Meta====&lt;br /&gt;
To add a client-side script, create a new text file inside your `my_first_script` folder and name it `client.lua`. Then, update your `meta.xml` to tell the server about this new client file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;meta&amp;gt;&lt;br /&gt;
     &amp;lt;info author=&amp;quot;YourName&amp;quot; type=&amp;quot;script&amp;quot; name=&amp;quot;My First Script&amp;quot; description=&amp;quot;Simple weather script&amp;quot; /&amp;gt;&lt;br /&gt;
     &amp;lt;script src=&amp;quot;script.lua&amp;quot; type=&amp;quot;server&amp;quot; /&amp;gt;&lt;br /&gt;
     &amp;lt;script src=&amp;quot;client.lua&amp;quot; type=&amp;quot;client&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/meta&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Writing the Client-Side Command====&lt;br /&gt;
Open `client.lua` and add the following code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function localWeatherCommand(commandName, weatherIDText)&lt;br /&gt;
    local weatherNumber = tonumber(weatherIDText)&lt;br /&gt;
&lt;br /&gt;
    if not weatherNumber then&lt;br /&gt;
        outputChatBox(&amp;quot;Syntax: /myweather [ID number]&amp;quot;)&lt;br /&gt;
        return&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    setWeather(weatherNumber)&lt;br /&gt;
    outputChatBox(&amp;quot;You changed your local weather!&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
addCommandHandler(&amp;quot;myweather&amp;quot;, localWeatherCommand)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice a big difference? In client-side commands, we don't need `thePlayer` as the first function argument. This is because on the client side, the script is only running for one person, so the player element is always automatically the [[localPlayer]]. &lt;br /&gt;
&lt;br /&gt;
Now, typing `/setweather 8` (Server) changes the sky for everyone, but typing `/myweather 8` (Client) changes it only for you!&lt;br /&gt;
&lt;br /&gt;
---&lt;br /&gt;
&lt;br /&gt;
==Step 4: Your Best Friend (Debugging)==&lt;br /&gt;
As you write your own scripts, you will inevitably make mistakes. If you misspell a function name or forget a closing parenthesis, your script simply will not work. &lt;br /&gt;
&lt;br /&gt;
However, debugging is not just for typos or syntax errors—it can show other errors too, like logic errors, passing the wrong type of data (e.g., passing text instead of a number), or trying to modify a vehicle or player element that has already disconnected or been destroyed.&lt;br /&gt;
&lt;br /&gt;
Instead of guessing what went wrong, you should use MTA's built-in script monitor.&lt;br /&gt;
&lt;br /&gt;
===Enabling Debugscript===&lt;br /&gt;
In the game client, open your console (usually by pressing the `F8` key or the `~` key) and type:&lt;br /&gt;
  debugscript 3&lt;br /&gt;
&lt;br /&gt;
A small transparent box will appear at the bottom of your screen. The `3` tells MTA to show all Errors, Warnings, and Information messages in real-time.&lt;br /&gt;
&lt;br /&gt;
====Seeing it in action====&lt;br /&gt;
Let us intentionally make a typo to see how `/debugscript 3` helps us. Open your `script.lua` and change [[setWeather]] to a misspelled word:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function brokenCommand()&lt;br /&gt;
    setWeatherrrr(8) -- Intentionally misspelled function&lt;br /&gt;
end&lt;br /&gt;
addCommandHandler(&amp;quot;brokeweather&amp;quot;, brokenCommand)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Save the file, restart your resource, and type `/brokeweather` in-game. &lt;br /&gt;
&lt;br /&gt;
Look at the bottom of your screen. You will see a red error message pop up:&lt;br /&gt;
  ERROR: my_first_script/script.lua:2: attempt to call global 'setWeatherrrr' (a nil value)&lt;br /&gt;
&lt;br /&gt;
This debug line tells you exactly where the problem is: inside the `my_first_script` folder, in `script.lua`, on '''line 2'''. It even tells you that `setWeatherrrr` is a &amp;quot;nil value&amp;quot; (meaning the engine doesn't recognize the word). &lt;br /&gt;
&lt;br /&gt;
Keeping `/debugscript 3` open while you code is the single most important habit you can build as an MTA developer.&lt;/div&gt;</summary>
		<author><name>Tails</name></author>
	</entry>
</feed>