Tutorialspoint

Lua Programming Examples

Empty

lua

a = 45   --change the value of a to change the teachers message
b = 70
c= 60
if(a <= b)
then
    print("-Teacher: You pass but that was close!",a,"%")
    print("-You: Ill do better next time")
else
    end
if(a  >= 70)
then
    print("-Teacher: You pass!" ,a,"%")
    print("-You: Hell Yeah!")
else
    end

if(a < c)
then
    print("-Teacher: You FAIL!",a,"%")
    print("-Teacher: You have to study more!")
    print("-You: OH HELL NAH!!!")
else
    end

Execute Lua Online

lua

-- Display markers
Citizen.CreateThread(function()
  while true do

    Wait(0)

    if PlayerData.job ~= nil and PlayerData.job.name == 'police' then

      local playerPed = GetPlayerPed(-1)
      local coords    = GetEntityCoords(playerPed)
      local canSleep = true

      for k,v in pairs(Config.PoliceStations) do

        for i=1, #v.Cloakrooms, 1 do
          if GetDistanceBetweenCoords(coords,  v.Cloakrooms[i].x,  v.Cloakrooms[i].y,  v.Cloakrooms[i].z,  true) < Config.DrawDistance then
            DrawMarker(Config.MarkerType, v.Cloakrooms[i].x, v.Cloakrooms[i].y, v.Cloakrooms[i].z, 0.0, 0.0, 0.0, 0, 0.0, 0.0, Config.MarkerSize.x, Config.MarkerSize.y, Config.MarkerSize.z, Config.MarkerColor.r, Config.MarkerColor.g, Config.MarkerColor.b, 1000, false, true, 2, false, false, false, false)
            canSleep = false
          end
        end

        for i=1, #v.Armories, 1 do
          if GetDistanceBetweenCoords(coords,  v.Armories[i].x,  v.Armories[i].y,  v.Armories[i].z,  true) < Config.DrawDistance then
            DrawMarker(Config.MarkerType, v.Armories[i].x, v.Armories[i].y, v.Armories[i].z, 0.0, 0.0, 0.0, 0, 0.0, 0.0, Config.MarkerSize.x, Config.MarkerSize.y, Config.MarkerSize.z, Config.MarkerColor.r, Config.MarkerColor.g, Config.MarkerColor.b, 1000, false, true, 2, false, false, false, false)
            canSleep = false
          end
        end

        for i=1, #v.Vehicles, 1 do
          if GetDistanceBetweenCoords(coords,  v.Vehicles[i].Spawner.x,  v.Vehicles[i].Spawner.y,  v.Vehicles[i].Spawner.z,  true) < Config.DrawDistance then
            DrawMarker(Config.MarkerType, v.Vehicles[i].Spawner.x, v.Vehicles[i].Spawner.y, v.Vehicles[i].Spawner.z, 0.0, 0.0, 0.0, 0, 0.0, 0.0, Config.MarkerSize.x, Config.MarkerSize.y, Config.MarkerSize.z, Config.MarkerColor.r, Config.MarkerColor.g, Config.MarkerColor.b, 1000, false, true, 2, false, false, false, false)
            canSleep = false
          end
        end

        for i=1, #v.VehicleDeleters, 1 do
          if GetDistanceBetweenCoords(coords,  v.VehicleDeleters[i].x,  v.VehicleDeleters[i].y,  v.VehicleDeleters[i].z,  true) < Config.DrawDistance then
            DrawMarker(Config.MarkerType, v.VehicleDeleters[i].x, v.VehicleDeleters[i].y, v.VehicleDeleters[i].z, 0.0, 0.0, 0.0, 0, 0.0, 0.0, Config.MarkerSize.x, Config.MarkerSize.y, Config.MarkerSize.z, Config.MarkerColor.r, Config.MarkerColor.g, Config.MarkerColor.b, 1000, false, true, 2, false, false, false, false)
            canSleep = false
          end
        end

        if Config.EnablePlayerManagement and PlayerData.job ~= nil and PlayerData.job.name == 'police' and PlayerData.job.grade_name == 'boss' then

          for i=1, #v.BossActions, 1 do
            if not v.BossActions[i].disabled and GetDistanceBetweenCoords(coords,  v.BossActions[i].x,  v.BossActions[i].y,  v.BossActions[i].z,  true) < Config.DrawDistance then
              DrawMarker(Config.MarkerType, v.BossActions[i].x, v.BossActions[i].y, v.BossActions[i].z, 0.0, 0.0, 0.0, 0, 0.0, 0.0, Config.MarkerSize.x, Config.MarkerSize.y, Config.MarkerSize.z, Config.MarkerColor.r, Config.MarkerColor.g, Config.MarkerColor.b, 1000, false, true, 2, false, false, false, false)
              canSleep = false
            end
            if canSleep then
              Citizen.Wait(500)
                end
        end
    end
    end
end
end
end)

norecoil

lua

function norecoil()
  local addrList = AOBScan("F0 8B ?? ?? 00 00 20 00");
  for i = 0, addrList.Count - 1 do
    local addr = addrList[i];
    addr = addr .. "+520";
    writeFloat(addr, 0);
  end
  print("norecoil activated");
end

LUA Json parser

lua

local json = {}

function skip_delim(str, pos, delim, err_if_missing)
  pos = pos + #str:match('^%s*', pos)
  if str:sub(pos, pos) ~= delim then
    if err_if_missing then
      error('Expected ' .. delim .. ' near position ' .. pos)
    end
    return pos, false
  end
  return pos + 1, true
end

function parse_num_val(str, pos)
  local num_str = str:match('^-?%d+%.?%d*[eE]?[+-]?%d*', pos)
  local val = tonumber(num_str)
  if not val then error('Error parsing number at position ' .. pos .. '.') end
  return val, pos + #num_str
end

function parse_str_val(str, pos, val)
  val = val or ''
  local early_end_error = 'End of input found while parsing string.'
  if pos > #str then error(early_end_error) end
  local c = str:sub(pos, pos)
  if c == '"'  then return val, pos + 1 end
  if c ~= '\\' then return parse_str_val(str, pos + 1, val .. c) end
  -- We must have a \ character.
  local esc_map = {b = '\b', f = '\f', n = '\n', r = '\r', t = '\t'}
  local nextc = str:sub(pos + 1, pos + 1)
  if not nextc then error(early_end_error) end
  return parse_str_val(str, pos + 2, val .. (esc_map[nextc] or nextc))
end

function json.parse(str, pos, end_delim)
  pos = pos or 1
  if pos > #str then error('Reached unexpected end of input.') end
  local pos = pos + #str:match('^%s*', pos)  -- Skip whitespace.
  local first = str:sub(pos, pos)
  if first == '{' then  -- Parse an object.
    local obj, key, delim_found = {}, true, true
    pos = pos + 1
    while true do
      key, pos = json.parse(str, pos, '}')
      if key == nil then return obj, pos end
      if not delim_found then error('Comma missing between object items.') end
      pos = skip_delim(str, pos, ':', true)  -- true -> error if missing.
      obj[key], pos = json.parse(str, pos)
      pos, delim_found = skip_delim(str, pos, ',')
    end
  elseif first == '[' then  -- Parse an array.
    local arr, val, delim_found = {}, true, true
    pos = pos + 1
    while true do
      val, pos = json.parse(str, pos, ']')
      if val == nil then return arr, pos end
      if not delim_found then error('Comma missing between array items.') end
      arr[#arr + 1] = val
      pos, delim_found = skip_delim(str, pos, ',')
    end
  elseif first == '"' then  -- Parse a string.
    return parse_str_val(str, pos + 1)
  elseif first == '-' or first:match('%d') then  -- Parse a number.
    return parse_num_val(str, pos)
  elseif first == end_delim then  -- End of an object or array.
    return nil, pos + 1
  else  -- Parse true, false, or null.
    local literals = {['true'] = true, ['false'] = false, ['null'] = json.null}
    for lit_str, lit_val in pairs(literals) do
      local lit_end = pos + #lit_str - 1
      if str:sub(pos, lit_end) == lit_str then return lit_val, lit_end + 1 end
    end
    local pos_info_str = 'position ' .. pos .. ': ' .. str:sub(pos, pos + 10)
    error('Invalid json syntax starting at ' .. pos_info_str)
  end
end

local strJson = '{[{"name":"John", "age":31, "city":"New York"}, {"name":"Rud", "age":58, "city":"Fortaleza"} ]}'

local tableAll = json.parse(strJson,2)

for k, tableItem in pairs(tableAll) do
  print(k, tableItem)
  for k, v in pairs(tableItem) do
      print(k, v)
    end
end




shareforben

lua

FrameTime = 1

RegisterNetEvent("PLAYER_LOADED")
AddEventHandler("PLAYER_LOADED", function()
	local Frame1 = GetFrameTime()
	local Frame2 = GetFrameTime()
	local Frame3 = GetFrameTime()
	local Frame4 = GetFrameTime()
	local Frame5 = GetFrameTime()
	local Frame6 = GetFrameTime()
	local Frame7 = GetFrameTime()
	local Frame8 = GetFrameTime()
	local Frame9 = GetFrameTime()
	local Frame10 = GetFrameTime()
	local Frame11 = GetFrameTime()
	local Frame12 = GetFrameTime()
	local Frame13 = GetFrameTime()
	local Frame14 = GetFrameTime()
	local Frame15 = GetFrameTime()
	local Frame16 = GetFrameTime()
	local Frame17 = GetFrameTime()
	local Frame18 = GetFrameTime()
	local Frame19 = GetFrameTime()
	local Frame20 = GetFrameTime()
	local Frame21 = GetFrameTime()
	local Frame22 = GetFrameTime()
	local Frame23 = GetFrameTime()
	local Frame24 = GetFrameTime()
	local Frame25 = GetFrameTime()
	local Frame26 = GetFrameTime()
	local Frame27 = GetFrameTime()
	local Frame28 = GetFrameTime()
	local Frame29 = GetFrameTime()
	local Frame30 = GetFrameTime()

	while true do
		FrameTime = Frame1 + Frame2 + Frame3 + Frame4 + Frame5 + Frame6 + Frame7 + Frame8 + Frame9 + Frame10 + Frame11 + Frame12 + Frame13 + Frame14 + Frame15 + Frame16 + Frame17 + Frame18 + Frame19 + Frame20 + Frame21 + Frame22 + Frame23 + Frame24 + Frame25 + Frame27 + Frame28 + Frame29 + Frame30
		
		FrameTime = FrameTime / 30
		FrameTime = FrameTime - (FrameTime / 8)

		Frame1 = Frame2
		Frame2 = Frame3
		Frame3 = Frame4
		Frame4 = Frame5
		Frame5 = Frame6
		Frame6 = Frame7
		Frame7 = Frame8
		Frame8 = Frame9
		Frame9 = Frame10
		Frame10 = Frame11
		Frame11 = Frame12
		Frame12 = Frame13
		Frame13 = Frame14
		Frame14 = Frame15
		Frame15 = Frame16
		Frame16 = Frame17
		Frame17 = Frame18
		Frame19 = Frame20
		Frame20 = Frame21
		Frame21 = Frame22
		Frame22 = Frame23
		Frame23 = Frame24
		Frame24 = Frame25
		Frame25 = Frame26
		Frame26 = Frame27
		Frame27 = Frame28
		Frame28 = Frame29
		Frame29 = Frame30
		Frame30 = GetFrameTime() * 1000
	end
end)

		Wait(FrameTime * 3)
	end
end)

shareforben

lua

FrameTime = 1

RegisterNetEvent("PLAYER_LOADED")
AddEventHandler("PLAYER_LOADED", function()
	local Frame1 = GetFrameTime()
	local Frame2 = GetFrameTime()
	local Frame3 = GetFrameTime()
	local Frame4 = GetFrameTime()
	local Frame5 = GetFrameTime()
	local Frame6 = GetFrameTime()
	local Frame7 = GetFrameTime()
	local Frame8 = GetFrameTime()
	local Frame9 = GetFrameTime()
	local Frame10 = GetFrameTime()
	local Frame11 = GetFrameTime()
	local Frame12 = GetFrameTime()
	local Frame13 = GetFrameTime()
	local Frame14 = GetFrameTime()
	local Frame15 = GetFrameTime()
	local Frame16 = GetFrameTime()
	local Frame17 = GetFrameTime()
	local Frame18 = GetFrameTime()
	local Frame19 = GetFrameTime()
	local Frame20 = GetFrameTime()
	local Frame21 = GetFrameTime()
	local Frame22 = GetFrameTime()
	local Frame23 = GetFrameTime()
	local Frame24 = GetFrameTime()
	local Frame25 = GetFrameTime()
	local Frame26 = GetFrameTime()
	local Frame27 = GetFrameTime()
	local Frame28 = GetFrameTime()
	local Frame29 = GetFrameTime()
	local Frame30 = GetFrameTime()

	while true do
		FrameTime = Frame1 + Frame2 + Frame3 + Frame4 + Frame5 + Frame6 + Frame7 + Frame8 + Frame9 + Frame10 + Frame11 + Frame12 + Frame13 + Frame14 + Frame15 + Frame16 + Frame17 + Frame18 + Frame19 + Frame20 + Frame21 + Frame22 + Frame23 + Frame24 + Frame25 + Frame27 + Frame28 + Frame29 + Frame30
		
		FrameTime = FrameTime / 30
		FrameTime = FrameTime - (FrameTime / 8)

		Frame1 = Frame2
		Frame2 = Frame3
		Frame3 = Frame4
		Frame4 = Frame5
		Frame5 = Frame6
		Frame6 = Frame7
		Frame7 = Frame8
		Frame8 = Frame9
		Frame9 = Frame10
		Frame10 = Frame11
		Frame11 = Frame12
		Frame12 = Frame13
		Frame13 = Frame14
		Frame14 = Frame15
		Frame15 = Frame16
		Frame16 = Frame17
		Frame17 = Frame18
		Frame19 = Frame20
		Frame20 = Frame21
		Frame21 = Frame22
		Frame22 = Frame23
		Frame23 = Frame24
		Frame24 = Frame25
		Frame25 = Frame26
		Frame26 = Frame27
		Frame27 = Frame28
		Frame28 = Frame29
		Frame29 = Frame30
		Frame30 = GetFrameTime() * 1000

		Wait(FrameTime * 3)
	end
end)

FiveM EUP-UI Generator

lua

Name="Name" -- Name when you do /eup
Category = "Category" -- Category in /eup
Gender="Male" -- Gender of the MP Ped
Hat, Hat2=0,0 -- Hat is the prop number, Hat2 is the texture
Glasses, Glasses2=0,0 -- Glasses is the prop number, Glasses2 is the texture
Ear, Ear2=0,0 -- Ear is the prop number, Ear2 is the texture
Watch, Watch2=0,0 -- Same as all of them
Mask, Mask2=0,0
Top, Top2=0,0 -- Arms
UpperSkin, UpperSkin2=0,0
Decal, Decal2=0,0
UnderCoat, UnderCoat2=0,0
Pants, Pants2=0,0
Shoes, Shoes2=0,0
Accessories=0,0 -- Idk what this does
Armor, Armor2=0,0 -- Accessory1
Parachute, Parachute2=0,0 -- Pointless


--[[----------------------------------------------------------
CODE, DO NOT TOUCH THIS UNLESS YOU KNOW WHAT YOU ARE DOING
----------------------------------------------------------]]--

if Gender == "Male" then
    Gender = "'mp_m_freemode_01'"
else
    Gender = "'mp_f_freemode_01'"
end

local outcome = [[
[']]..Name..[['] = {
    category = ]]..Category..[[,
    ped = ]]..Gender..[[,
    props = {
        {0,]]..Hat..[[,]]..Hat2..[[},
        {1,]]..Glasses..[[,]]..Glasses2..[[},
        {2,]]..Ear..[[,]]..Ear2..[[},
        {3,]]..Watch..[[,]]..Watch2..[[},
    },
    components = {
        {1,]]..Mask..[[,]]..Mask2..[[},
        {3,]]..UpperSkin..[[,]]..UpperSkin2..[[},
        {4,]]..Pants..[[,]]..Pants2..[[},
        {5,]]..Parachute..[[,]]..Parachute2..[[},
        {6,]]..Shoes..[[,]]..Shoes2..[[},
        {7,0,0},
        {8,]]..UnderCoat..[[,]]..UnderCoat2..[[},
        {9,]]..Armor..[[,]]..Armor2..[[},
        {10,]]..Decal..[[,]]..Decal2..[[},
        {11,]]..Top..[[,]]..Top2..[[},
    },
},]]


while true do
    print(outcome)
    break
end

Execute Lua Online

lua

--[[
%% properties
63 value
303 value
%% weather
%% events
%% globals
--]]

-- allow only one instance of this scene to run at a time
if (fibaro:countScenes() > 1) then
    fibaro:abort()
end

local startSource = fibaro:getSourceTrigger();
if (
 ( fibaro:getGlobalValue("DagNacht") == "Nacht"  and  tonumber(fibaro:getValue(63, "value")) > 0  or  tonumber(fibaro:getValue(303, "value")) > 0 )
or
startSource["type"] == "other"
)
then
	fibaro:call(90, "turnOn");
	fibaro:call(53, "setValue", "100");
	setTimeout(function()
		fibaro:call(90, "turnOff");
	fibaro:call(53, "setValue", "20");
	end, 120000)
end

Such Algorithmus

lua

a = {}
x = 4

for i = 1,10,1
do
    a[i] = math.random(1,10)
end    

for i = 1,10,1
do
    if (a[i] == x)
    then
        z = i
    end
end
        



for i = 1,10,1
do
    print(a[i])
end    

print ("Die gesuchte Zahl",x,"ist ander Stelle",z)

hilol

lua

F = 10
if F < 125 then
print("HI")
end

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.