Author Topic: Begin your journey with Lua!  (Read 165 times)

0 Members and 1 Guest are viewing this topic.

Offline remaked

  • [D3] Developer
  • ***
    • View Profile
Begin your journey with Lua!
« on: September 30, 2020, 09:30:27 am »
Hello everyone, recently I've been receiving a few DM's about how to learn scripting etc..., so I decided to open this topic to maybe provide help to those who are willing to learn Lua and maybe push those who are just thinking about it and hesitating.

Part 1:

First of all, I can't explain better than those links, so you should read them carefully before reading the next part of this topic:

1- https://wiki.multitheftauto.com/wiki/Scripting_Introduction
2- https://www.lua.org/manual/5.1/
3- https://forum.mtasa.com/topic/34453-lua-tutorials-manuals/

After reading those links carefully, you should now know what 'debugging' is. Infact, debugging may help you fix the errors yourself 99% of the time, here is a link to know what debugging is:
- https://wiki.multitheftauto.com/wiki/Debugging

Now you can go ahead and save this link in your browser: https://wiki.multitheftauto.com/wiki/Main_Page

The last link is the most important of all, it's where you'll be looking every time you are making something(a script).

Part 2: (Only read this if you totally understood/read Part 1)

Now, you should have a general idea about scripting/Lua, so we'll be making a couple scripts to get you started with.

We are gonna make a script which outputs "blabla logged in" whenever a player logs in, and another for when a player logs out.

First things first, we are gonna go here(https://wiki.multitheftauto.com/wiki/Server_Scripting_Events) and look for an event that triggers when a player logs in, which is gonna be "onPlayerLogin". Let's go:
Code: (Lua) [Select]
function playerLogin() -- function name should always be camel case(this is only for Lua)
local playerName = getPlayerName(source) -- get the name of the player who logged in with "getPlayerName"
outputChatBox("JOINQUIT: "..playerName.." has logged in!", root) -- We output a text to everyone online using "outputChatBox"
addEventHandler("onPlayerLogin", root, playerLogin) -- Our event handler which will trigger this function every time a player logs in

For when a player logs out, we will need an event for that, we go to wiki and we will find "onPlayerLogout", let's go:

Code: (Lua) [Select]
function playerLogout() -- function name should always be camel case(this is only for Lua)
local playerName = getPlayerName(source) -- Here we are getting the name of the player who logged out with "getPlayerName"
outputChatBox("JOINQUIT: "..playerName.." has logged out!", root) -- We output a text to everyone onine using "outputChatBox"
addEventHandler("onPlayerLogout", root, playerLogout) -- Our event which will trigger this function every time a player logs out

Now let's make a script which will give a random player 50,000$ everytime you type /giverandomcash and output a message to the random player to inform him about the money.
We go to the wiki(as always) and we search for a function to get a random player, which will be https://wiki.multitheftauto.com/wiki/GetRandomPlayer, and a function to give the player money, which will be https://wiki.multitheftauto.com/wiki/GivePlayerMoney, finally to inform we will use outputChatBox which we have seen earlier. Let's go!

Code: (Lua) [Select]
function giveRandomMoney(thePlayer, command)
local randomPlayer = getRandomPlayer() -- we get a random player
givePlayerMoney(randomPlayer, 50000) -- we give the money
outputChatBox("You got lucky and won 50,000$", randomPlayer, 0, 255, 0) -- we output a text to the player to inform him about the money
addCommandHandler("giverandomcash", giveRandomMoney) -- Our function will trigger once this you type this command

Part 3:
Here are two tasks for you to test yourself!
- Make a script which removes the player's head once he executes the command '/head', and give it back again when he executes the same command.

Task 2:
- Create a pickup and make it that when a player enters the pickup, he will get 1,000$

You can then post your work here, same if you face any errors/questions, and someone(or me) will hopefully answer you
\"We need to love one another, live in peace and harmony. Instead, we quarrel and fight.\"


SimplePortal 2.3.7 © 2008-2020, SimplePortal