Jump to content

[TUT] Simple LUA tutorial 1


will briggs

Recommended Posts

Simple LUA tutorial 1

Hey if you've come here, then you have clicked to learn how to make simple things like : Local Chats, Simple Jobs

So Lets get started!

Tutorial one : How to Create a Local Chat

The Meta

First of all we need to sort out the meta which tells MTA that this resource exists, and these are the files.

Create a file called 'meta.xml'

The Meta should look like this :

<meta> 
    <info name="Local Chat" author="Will" version="1.1" type="misc" /> 
    <script src="localchat.lua" type="server"/> 
</meta> 

Now lets go through this bit by bit :)

The first 'tag' is the meta tag and it looks like this :

<meta></meta> 

Everything that is in these tags will be looked at my the MTA server console. But we need some content to go in the meta.

So we add some info to it as such :

    <info name="Local Chat" author="Will" version="1.1" type="misc" /> 

As you can see - im stating who its made by, the name, the version and the type of resource it is.

Now the important bit, you need to know how to add your files which is :

    <script src="localchat.lua" type="server"/> 

This defines what the script is called and what type it is - weather it be server or client.

The server script

This is the point when you need to make a file called 'localchat.lua' in the same folder as the meta file.

Open up the Server file and start to read this :

First we need to set a variable for the chat range

chat_range=100 

That defines where the chat starts and cuts off centred around you.

Now we need to add that when the player joins, it binds the key 'u' down to local chat.

chat_range=100 
  
addEventHandler("onPlayerJoin",getRootElement(), 
function () 
bindKey(source,"u","down","chatbox","LocalChat") 
end) 

This now when a player joins, binds the key 'u' on key down to the chatbox and more specificly to 'locachat'

That's called an event hense 'addEventHandler'

But what if the resource has just been started :o

We dont want everyone to rejoin to access local chat do we So we need to add a new event that will handle this

addEventHandler("onResourceStart",getResourceRootElement(getThisResource()), 
function () 
for index, player in pairs(getElementsByType("player")) do 
bindKey(player,"u","down","chatbox","LocalChat") 
  end 
end) 

This is the event - It's self explanatory if you read the event above this one.

If we add it into our overall script it looks like this :

chat_range=100 
  
addEventHandler("onPlayerJoin",getRootElement(), 
function () 
bindKey(source,"u","down","chatbox","LocalChat") 
end) 
  
addEventHandler("onResourceStart",getResourceRootElement(getThisResource()), 
function () 
for index, player in pairs(getElementsByType("player")) do 
bindKey(player,"u","down","chatbox","LocalChat") 
  end 
end) 

Now we need to add a function that handles weather a player is in anotherones area.

function isPlayerInRangeOfPoint(player,x,y,z,range) 
   local px,py,pz=getElementPosition(player) 
   return ((x-px)^2+(y-py)^2+(z-pz)^2)^0.5<=range 
end 
  

If we add that into the main script - it looks like this :

chat_range=100 
  
addEventHandler("onPlayerJoin",getRootElement(), 
function () 
bindKey(source,"u","down","chatbox","LocalChat") 
end) 
  
addEventHandler("onResourceStart",getResourceRootElement(getThisResource()), 
function () 
for index, player in pairs(getElementsByType("player")) do 
bindKey(player,"u","down","chatbox","LocalChat") 
  end 
end) 
  
function isPlayerInRangeOfPoint(player,x,y,z,range) 
   local px,py,pz=getElementPosition(player) 
   return ((x-px)^2+(y-py)^2+(z-pz)^2)^0.5<=range 
end 
  

This last part to the script handles the actual chat of a player and displays it in the chatbox :

function onChat(player,_,...) 
  local px,py,pz=getElementPosition(player) 
  local msg = table.concat({...}, " ") 
  local nick=getPlayerName(player) 
local r,g,b = getTeamColor(getPlayerTeam(player)) 
  for _,v in ipairs(getElementsByType("player")) do 
    if isPlayerInRangeOfPoint(v,px,py,pz,chat_range) then 
      outputChatBox("(LocalChat)"..nick..": #ffffff"..msg,v,r,g,b,true) 
    end 
  end 
end 
addCommandHandler("LocalChat",onChat) 

Theres allot going on up there so let me explain this

It starts by defining variables and then moves onto grabbing the players team colour, after that it see's weather the player is in range, if they are, it carries on to send a message out starting with '(localchat') then with your name and the message.

Then it ends it all and adds the command handler 'Local Chat'

Finished Script :

chat_range=100 
  
addEventHandler("onPlayerJoin",getRootElement(), 
function () 
bindKey(source,"u","down","chatbox","LocalChat") 
end) 
  
addEventHandler("onResourceStart",getResourceRootElement(getThisResource()), 
function () 
for index, player in pairs(getElementsByType("player")) do 
bindKey(player,"u","down","chatbox","LocalChat") 
  end 
end) 
  
function isPlayerInRangeOfPoint(player,x,y,z,range) 
   local px,py,pz=getElementPosition(player) 
   return ((x-px)^2+(y-py)^2+(z-pz)^2)^0.5<=range 
end 
  
function onChat(player,_,...) 
  local px,py,pz=getElementPosition(player) 
  local msg = table.concat({...}, " ") 
  local nick=getPlayerName(player) 
local r,g,b = getTeamColor(getPlayerTeam(player)) 
  for _,v in ipairs(getElementsByType("player")) do 
    if isPlayerInRangeOfPoint(v,px,py,pz,chat_range) then 
      outputChatBox("(LocalChat)"..nick..": #ffffff"..msg,v,r,g,b,true) 
    end 
  end 
end 
addCommandHandler("LocalChat",onChat) 
  

Finally save it all and run it on your MTA SA server, then press u to use it :)

Hope this helped you all - i will be adding more tutorials as we get on :)

Cya next time

Edited by Guest
Link to comment
  • 3 months later...
  • Recently Browsing   0 members

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