Jump to content

[TUT] Simple LUA tutorial 2


will briggs

Recommended Posts

Simple LUA tutorial 2

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

Before that - Why don't you check our some of my other tutorials?

Tutorial 1 : Local Chat

So Lets get started!

Tutorial two : How to Create a Bus Driver Role

This tutorial is most suited to people who want to create RPG gamemodes.

The Meta

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

Im only going to show the full meta here - If you want to view what each line does - Go to tutorial 1

<meta> 
    <info author="Will" description="Bus Role" version="1" type="misc"/> 
    <script src="bus.lua"/> 
    <script src="bus_c.lua" type="client"/> 
</meta> 

The server script

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

Open up the Server file and start to read this :

First of all we are going to define what buses the role will work in :

local rootElement = getRootElement() 
local Bus = createTeam("Bus Driver", 0, 255, 255) 
local busses = {[431] = true, [437] = true} 
  

431 and 437 are the ID's for a bus, and a coach. If you need to find some other ID's go to this link at the wiki Click me! Also the local Bus = createTeam creates a team thats yellow (i think) called 'Bus Driver'

So Now the next thing is to define the bus route positions :

local busTable = { 
[1]={1812.65198, -1889.86047, 13.41406}, 
[2]={1825.22791, -1635.03711, 13.38281}, 
} 

Im only giving 2 as an example - and these are located at the LS Train Station

If you want to add more just follow the pattern of [1]={1812.65198, -1889.86047, 13.41406}, but change the coordinates and number at the start of the line.

The next thing that we need to do is grab the bus locations from the table and trigger a client event (in the client script) that will set the location and add a blip and marker ect...

function getNewBusLocation(thePlayer, ID) 
local x, y, z = busTable[ID][1], busTable[ID][2], busTable[ID][3] 
triggerClientEvent(thePlayer,"bus_set_location",thePlayer,x,y,z) 
end 

Next we are going to set that when a player enters the bus it checks what team they are in and if the team is Bus Driver then it plots the route accordingly.

function onVehicleEnter(thePlayer) 
if not busses[getElementModel(source)] then return end 
if not getPlayerTeam(thePlayer) then return end 
if getTeamName(getPlayerTeam(thePlayer)) == "Bus Driver" then 
local x, y, z = getNewBusLocation(thePlayer, 1) 
setElementData(thePlayer,"busData",1) 
  end 
end 
addEventHandler("onVehicleEnter",rootElement,onVehicleEnter) 
  

This next part checks weather the player is in the vehicle - and if they are when the marker is hit, gives them $100. So for every marker thats hit the player gets $100.

addEvent("bus_finish",true) 
addEventHandler("bus_finish",rootElement, 
function (client) 
if not isPedInVehicle(client) then return end 
if not busses[getElementModel(getPedOccupiedVehicle(client))] then return end 
givePlayerMoney(client, 100) 
if #busTable == tonumber(getElementData(client,"busData")) then 
setElementData(client,"busData",1) 
else 
setElementData(client,"busData",tonumber(getElementData(client,"busData"))+1) 
end 
getNewBusLocation(client, tonumber(getElementData(client,"busData"))) 
end) 

So the WHOLE server side is :

local rootElement = getRootElement() 
local Bus = createTeam("Bus Driver", 0, 255, 255) 
local busses = {[431] = true, [437] = true} 
  
local busTable = { 
[1]={1812.65198, -1889.86047, 13.41406}, 
[2]={1825.22791, -1635.03711, 13.38281}, 
} 
  
function getNewBusLocation(thePlayer, ID) 
local x, y, z = busTable[ID][1], busTable[ID][2], busTable[ID][3] 
triggerClientEvent(thePlayer,"bus_set_location",thePlayer,x,y,z) 
end 
  
function onVehicleEnter(thePlayer) 
if not busses[getElementModel(source)] then return end 
if not getPlayerTeam(thePlayer) then return end 
if getTeamName(getPlayerTeam(thePlayer)) == "Bus Driver" then 
local x, y, z = getNewBusLocation(thePlayer, 1) 
setElementData(thePlayer,"busData",1) 
  end 
end 
addEventHandler("onVehicleEnter",rootElement,onVehicleEnter) 
  
addEvent("bus_finish",true) 
addEventHandler("bus_finish",rootElement, 
function (client) 
if not isPedInVehicle(client) then return end 
if not busses[getElementModel(getPedOccupiedVehicle(client))] then return end 
givePlayerMoney(client, 100) 
if #busTable == tonumber(getElementData(client,"busData")) then 
setElementData(client,"busData",1) 
else 
setElementData(client,"busData",tonumber(getElementData(client,"busData"))+1) 
end 
getNewBusLocation(client, tonumber(getElementData(client,"busData"))) 
end) 

The client script

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

Ok so the next part here is the start of a new file called bus_c.lua thats a client script.

local client = getLocalPlayer( ) 
local rootElement = getRootElement() 
local marker = nil 
local blip = nil 
  
addEvent("bus_set_location",true) 
addEventHandler("bus_set_location",rootElement, 
function (x, y, z) 
marker = createMarker(tostring(x), tostring(y), tostring(z)-1, "cylinder", 3.5, 255, 255, 0, 170) 
blip = createBlipAttachedTo( marker, 0, 2, 255, 255, 0, 255 ) 
addEventHandler("onClientMarkerHit",marker,onBusStopHit) 
end) 

First of all the code above this line grabs the local player and then has a function bellow it. That function is refered to by the server side script to create a blip on the map and a marker on the world...

The next function i called on bus top hit. This handles when a bus collides with a marker. then it triggers a server event we added earlier.

function onBusStopHit(hitPlayer) 
if not hitPlayer == client then return end 
triggerServerEvent("bus_finish",client,client) 
if isElement(blip) then destroyElement(blip) end 
if isElement(marker) then 
removeEventHandler("onClientMarkerHit",marker,onBusStopHit) 
destroyElement(marker)  
  end 
end 
  

And now the last part to the client script is to create an event of when a player leaves the vehicle in a route.

addEventHandler("onClientVehicleExit",rootElement, 
function () 
if isElement(marker) then 
removeEventHandler("onClientMarkerHit",marker,onBusStopHit) 
destroyElement(marker)  
end 
if isElement(blip) then destroyElement(blip) end 
end) 

So when they do leave the vehicle - it removes the markers on blips on the map

So the Whole Client Script is :

local client = getLocalPlayer( ) 
local rootElement = getRootElement() 
local marker = nil 
local blip = nil 
  
addEvent("bus_set_location",true) 
addEventHandler("bus_set_location",rootElement, 
function (x, y, z) 
marker = createMarker(tostring(x), tostring(y), tostring(z)-1, "cylinder", 3.5, 255, 255, 0, 170) 
blip = createBlipAttachedTo( marker, 0, 2, 255, 255, 0, 255 ) 
addEventHandler("onClientMarkerHit",marker,onBusStopHit) 
end) 
  
function onBusStopHit(hitPlayer) 
if not hitPlayer == client then return end 
triggerServerEvent("bus_finish",client,client) 
if isElement(blip) then destroyElement(blip) end 
if isElement(marker) then 
removeEventHandler("onClientMarkerHit",marker,onBusStopHit) 
destroyElement(marker)  
  end 
end 
  
addEventHandler("onClientVehicleExit",rootElement, 
function () 
if isElement(marker) then 
removeEventHandler("onClientMarkerHit",marker,onBusStopHit) 
destroyElement(marker)  
end 
if isElement(blip) then destroyElement(blip) end 
end) 

So to use this job, you need to set your team to Bus Driver and then get in a coach or bus.

Thanks all - Regards - Will

Client Script

local client = getLocalPlayer( ) 
local rootElement = getRootElement() 
local marker = nil 
local blip = nil 
  
addEvent("bus_set_location",true) 
addEventHandler("bus_set_location",rootElement, 
function (x, y, z) 
marker = createMarker(tostring(x), tostring(y), tostring(z)-1, "cylinder", 3.5, 255, 255, 0, 170) 
blip = createBlipAttachedTo( marker, 0, 2, 255, 255, 0, 255 ) 
addEventHandler("onClientMarkerHit",marker,onBusStopHit) 
end) 
  
function onBusStopHit(hitPlayer) 
if not hitPlayer == client then return end 
triggerServerEvent("bus_finish",client,client) 
if isElement(blip) then destroyElement(blip) end 
if isElement(marker) then 
removeEventHandler("onClientMarkerHit",marker,onBusStopHit) 
destroyElement(marker)  
  end 
end 
  
addEventHandler("onClientVehicleExit",rootElement, 
function () 
if isElement(marker) then 
removeEventHandler("onClientMarkerHit",marker,onBusStopHit) 
destroyElement(marker)  
end 
if isElement(blip) then destroyElement(blip) end 
end) 

Server Script

local rootElement = getRootElement() 
local Bus = createTeam("Bus Driver", 0, 255, 255) 
local busses = {[431] = true, [437] = true} 
  
local busTable = { 
[1]={1812.65198, -1889.86047, 13.41406}, 
[2]={1825.22791, -1635.03711, 13.38281}, 
} 
  
function getNewBusLocation(thePlayer, ID) 
local x, y, z = busTable[ID][1], busTable[ID][2], busTable[ID][3] 
triggerClientEvent(thePlayer,"bus_set_location",thePlayer,x,y,z) 
end 
  
function onVehicleEnter(thePlayer) 
if not busses[getElementModel(source)] then return end 
if not getPlayerTeam(thePlayer) then return end 
if getTeamName(getPlayerTeam(thePlayer)) == "Bus Driver" then 
local x, y, z = getNewBusLocation(thePlayer, 1) 
setElementData(thePlayer,"busData",1) 
  end 
end 
addEventHandler("onVehicleEnter",rootElement,onVehicleEnter) 
  
addEvent("bus_finish",true) 
addEventHandler("bus_finish",rootElement, 
function (client) 
if not isPedInVehicle(client) then return end 
if not busses[getElementModel(getPedOccupiedVehicle(client))] then return end 
givePlayerMoney(client, 100) 
if #busTable == tonumber(getElementData(client,"busData")) then 
setElementData(client,"busData",1) 
else 
setElementData(client,"busData",tonumber(getElementData(client,"busData"))+1) 
end 
getNewBusLocation(client, tonumber(getElementData(client,"busData"))) 
end) 

Link to comment
  • 1 month later...
  • 1 month later...

iirc castillo and Will don't use indentation..

i dont put indentation my meta.xml (for resource with just a few files, lots of files = identation) but i cant script without identitation...

anyone who uses notepad to script NEEDS TO DIE.

Remi-X, indentation, not indentitation

Link to comment
  • Recently Browsing   0 members

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