Jump to content

Some questions about scripting in MTA


Wes

Recommended Posts

Hey! I just recently started working with LUA in a deeper view.

I have a few questions, if you could answer them that 'd be great.

1. When do you need to use getRootElement(), or getThisResource() ?

2. How can I use the for loop? I understand what it does, but its "build" is not understandable for me.

3. Is there a way to easily create GUI-s without messing with the Width, Height?

4. Why do I need to add that "command" argument in a function? like function giveMoney(thePlayer, command, amount)

5. How can I use functions in other files? So like I have a loginPlayer() function in login.lua, and I want to use it like in xy.lua?

6. nil means that something is empty? So if theres an argument that you have to add, this is how you can check if it is empty?

7. What operator is ~ this one? Cause I've never seen it anywhere else.

8. I don't really understand the sync between a Client and a Server side script. For example the triggerClientEvent and the triggerServerEvent.

9. I've seen something like that in the first part of the script, the guy used local to set his variables. But later on, he left the local stuff and he just set the variables without that. And it was not for the same stuff, so I think he wasnt changing the local variables value.

Thanks for the answers, and sorry for all these noob questions :D

Link to comment
  • Moderators

1: https://wiki.multitheftauto.com/wiki/Element_tree

2: You can use a loop for differed things.

here for index a table:

local myTable = {"A","B","C","D"} 
  
for i,data in pairs (myTable) do 
    setElementData ( localPlayer, data,1000) 
end 
  
 -- or 
  
for i=1,#myTable do 
    local data = myTable[i] 
    setElementData ( localPlayer, data,1000) 
end 

3: use relative when you want to use it easy.

4: you don't need to use a "command" argument. It is just how it is written down and the system works.

5:You can export information.

6: Check if something is nil or false

if not A then  -- check if [u]nil[/u] or [u]false[/u] 
if A == nil then -- check if [u]nil[/u] 
if not (A ~= nil) then -- check if [u]nil[/u] -- not recommended 
if A == false then -- check if [u]false[/u] 
if not  (A ~= false) then -- check if [u]false[/u] -- not recommended 
  

7: ~= = not/unequal

8:

Client = your pc

Server = the server you are playing on.

triggerClientEvent, server sends information to player/players

triggerServerEvent, player sends information to server

9:

local means that it will stay within the if/function/script/loops T/M end/end of the script

sample:

  
if 1 then -- 
local A = "hey" -- local value A 
outputChatBox(A) -- shows in the chat "hey" 
end -- A is gone 
outputChatBox(A) -- shows "nil" in the chat, because A will only stay between the lines. 

You use locals to improve the performance of the code, because the data is gone after the execution.

If it is outside the functions the data can be used only in the that .lua file.

Link to comment

Uh alright

And in the loop, for like in the example, can I use anything like data? or is it something like a data type? OR can I write anything there?

I found a nice gui editor, so I wont have problems with the GUI-s anymore.

I meant about the client / serverside that for example I have to trigger something? And if yes, how? Could you make a simple example, like a function in clientside and then a serverside? I'd appreciate that, and thank you for these informations, it clarifies my brain a lot :D

Link to comment
  • Moderators

This script removes a weapon from a slot, by triggering to server side.

client

addCommandHander("remove", 
function () 
local currentSlot = getPedWeaponSlot (localPlayer) 
triggerServerEvent("tkWP",localPlayer,currentSlot) 
end)  

server

addEvent("tkWP",true) 
addEventHandler ("tkWP",root, 
function(slot) 
    if isElement(source) then-- check if the player is still in the game 
        takeWeapon ( source, getPedWeapon ( source, slot)) 
    end 
end) 

Link to comment
  • Moderators

ah loop is something that goes through a table and does something with the data.

So you can read information very easy and it makes your code a lot smaller.

You can write everything there, but you have to put in the the table first.

table.insert (myTable,value) 
--or 
myTable[#myTable+1] = value 

Some functions are creating already a table. You can use loops to read the whole table.

Link to comment
ah loop is something that goes through a table and does something with the data.

So you can read information very easy and it makes your code a lot smaller.

You can write everything there, but you have to put in the the table first.

table.insert (myTable,value) 
--or 
myTable[#myTable+1] = value 

Wrong. Loop is a code structure which executes the block of code repeatedly as long as some condition is met. Yes, it can be used with a table, but it doesn't need to be.

Link to comment
  • Moderators

yes,

I wasn't able to find the correct words for it. :)

I only use it for tables or numbers from point to point to reading files. well let we say I using it at every single corner in my code.

Link to comment

Alright, okey

And in condition flow, do I have to add the word "end" after every statement?

Like this

function regisztracio(player, email, passReg, passAReg) 
  
    registered = getElementData(player, "registered") 
    if email and passReg and passAReg then 
        if not (registered) then 
            accountAdded = addAccount(email, pass) 
                if (accountAdded) then 
                    outputChatBox("Sikeresen regisztráltál a szerverre! Most jelentkezz be!") 
                     
                else 
                    outputChatBox("Nem sikerült a regisztráció!") 
                    end 
        else 
            outputChatBox("A megadott E-mail cím már regisztrálva van!") 
            end 
    else 
        outputChatBox("Add meg az E-mail címet és a jelszót") 
        end 
         
end 
  
end 
  
addEvent("regisztracioFunction", true) 
addEventHandler("regisztracioGomb", root, regisztracio) 

I dont know if this code works, I havent tested it yet, but where do I need to use the word "end"?

Link to comment
  • Moderators
function myFunction () 
  
end 
  
------------------ 
------------------ 
  
if a then 
  
end 
  
------------------ 
------------------ 
  
if  a then 
  
elseif b then 
  
end 
  
------------------ 
------------------ 
  
if a then 
  
elseif b then 
  
else 
  
end 
  
------------------ 
------------------ 
  
for i=1,2 do 
  
end 
  
------------------ 
------------------ 
  
if a then 
    if b then 
     
    end 
end 
  
------------------ 
------------------ 
  
function myFunction () 
    if a then 
        if b then 
         
        end 
    end 
end 

function myFunction () 
    if a then 
        if b then 
            outputChatBox("b  does exist") 
        elseif c then 
            outputChatBox("c  does exist") 
        else 
            outputChatBox("b and c does not exist") 
        end 
    else 
        outputChatBox("a  does not exist") 
    end 
end 
  

Link to comment

Alright I got it now, thank you very much :)

Why is that when I start the server it just a black screen, the login screen doesnt appear... How can I do that so when a player joins the login screen appears, and the camera is pointing in a way?

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...