Jump to content

Up-to-date Lua tutorials?


WallMart

Recommended Posts

Hi guys.

Back after a long time of in-activity on MTA, i decided i wanted to learn to script in Lua.

However the tutorials i found are a bit outdated, since the scripts ( i literally copied them, just to try it out ) don't work anymore.

If anyone could link me to any tutorial on lua that works with the current MTA, that would be nice.

Greets, WallMart

EDIT: i figured i should post the code too, maybe i did something horribly wrong.

local spawnX, spawnY, spawnZ = 1959.55, -1714.46, 10 
function joinHandler() 
    spawnPlayer(source, spawnX, spawnY, spawnZ) 
    fadeCamera(source, true) 
    setCameraTarget(source, source) 
    outputChatBox("Welcome to My Server", source) 
end 
addEventHandler("onPlayerJoin", getRootElement(), joinHandler) 

Link to comment
function spawn(player) 
   if getElementType(player)=="player" then 
      local desiredSkin = getElementData(player, "skinID") 
      if desiredSkin then 
         skinID=desiredSkin 
      else 
         skinID= math.random(9,288) 
      end 
      spawnX = math.random(-691, -671) 
      spawnY = math.random(962, 971) 
      spawnZ = 20 
      repeat 
      until 
      spawnPlayer ( player, spawnX, spawnY, spawnZ, 180, skinID ) 
  
      fadeCamera(player, true) 
      setCameraTarget(player, player) 
   end 
end 
  
addEventHandler("onPlayerJoin", root, 
   function() 
      setTimer(function(src) 
         spawn(src) 
      end, 5000, 1, source) 
   end 
) 
  
addEventHandler("onPlayerWasted", root, 
   function() 
      if getElementType(source)=="player" then 
         local tmpID = getPedSkin(source) 
         --outputChatBox(tmpID.."f") 
         setElementData(source,"skinID",tmpID) 
         setTimer(spawn, 5000, 1, source) 
      end 
   end 
) 

Link to comment
This is a server side code, do you have that in meta.xml? Like :

  
<meta> 
         <script src="s_name.lua" type="server"/> 
</meta> 
  

it still says that it contains illegal characters.

but as i said, i took this literally from an official tutorial of the mta wiki and it stated that it should work with those lines of code.

so if you an updated tutorial i would be grateful :D

Link to comment
function spawn(player) 
   if getElementType(player)=="player" then 
      local desiredSkin = getElementData(player, "skinID") 
      if desiredSkin then 
         skinID=desiredSkin 
      else 
         skinID= math.random(9,288) 
      end 
      spawnX = math.random(-691, -671) 
      spawnY = math.random(962, 971) 
      spawnZ = 20 
      repeat 
      until 
      spawnPlayer ( player, spawnX, spawnY, spawnZ, 180, skinID ) 
  
      fadeCamera(player, true) 
      setCameraTarget(player, player) 
   end 
end 
  
addEventHandler("onPlayerJoin", root, 
   function() 
      setTimer(function(src) 
         spawn(src) 
      end, 5000, 1, source) 
   end 
) 
  
addEventHandler("onPlayerWasted", root, 
   function() 
      if getElementType(source)=="player" then 
         local tmpID = getPedSkin(source) 
         --outputChatBox(tmpID.."f") 
         setElementData(source,"skinID",tmpID) 
         setTimer(spawn, 5000, 1, source) 
      end 
   end 
) 

Use of element data here is quite bad, passing the skin id as function argument thru timer would be much better.

Link to comment

okay, can someone help me with this script? it's the first one i have written myself, obviously it doesnt work :P

i wanted to make a script, that when players press the R key their car will be completely repaired.

function playerPressedKey (r, press) 
    local theVehicle = getPedOccupiedVehicle ( thePlayer ) 
    if (press) then 
    setElementHealth (theVehicle, getElementHealth(theVehicle) + 100) 
    end 
end 
addEventHandler ("setElementHealth", root, getPedOccupiedVehicle) 

it's nice if anyone could fix the code, it would be even better with explanation ( and tell me if i really fucked up, i just want to learn :) )

Link to comment

Your code doesn't make much sense ;). Try this, on client side. And if you need further explaination then ask just ask for it!

function repairCar ()    
    local vehicle = getPedOccupiedVehicle ( localPlayer ) 
    if vehicle then -- check if the local player is in a vehicle 
        fixVehicle ( vehicle ) -- fix it 
    end 
end 
  
bindKey ( "r", "down", repairCar ) -- bind a key to a function 

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...