ESX = nil
Citizen.CreateThread(function()
    while ESX == nil do
        TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
        Citizen.Wait(0)
    end
	PlayerLoaded = true
	ESX.PlayerData = ESX.GetPlayerData()
end)

RegisterKeyMapping('revive:menu', Translation[config.Locale]['revive:menu_desc'], 'keyboard', 'insert')

RegisterCommand('revive:menu', function(source, args, rawCommand)
    openMenu()
end)

RegisterNetEvent('esx:playerLoaded')
AddEventHandler('esx:playerLoaded', function(xPlayer)
	ESX.PlayerData = xPlayer
	PlayerLoaded = true
end)

_menuPool = NativeUI.CreatePool()
local mainMenu

local isBusy = false
local PlayerLoaded = false


Citizen.CreateThread(function()
    while true do

        _menuPool:ProcessMenus()


        local playerPed = PlayerPedId()
        local playerCoords = GetEntityCoords(playerPed)
        
        Citizen.Wait(1)
    end
end)

function openMenu()
    mainMenu = NativeUI.CreateMenu(Translation[config.Locale]['title'], Translation[config.Locale]['desc'])
    _menuPool:Add(mainMenu)

    local reviveItem = NativeUI.CreateItem(Translation[config.Locale]['revive_title'], Translation[config.Locale]['revive_desc'])
    mainMenu:AddItem(reviveItem)

    local healBigItem = NativeUI.CreateItem(Translation[config.Locale]['heal_big_title'], Translation[config.Locale]['heal_big_desc'])
    mainMenu:AddItem(healBigItem)

    local healSmallItem = NativeUI.CreateItem(Translation[config.Locale]['heal_small_title'], Translation[config.Locale]['heal_small_desc'])
    mainMenu:AddItem(healSmallItem)

    reviveItem.Activated = function(sender, index)
        revivePlayer(closestPlayer)
    end

    healBigItem.Activated = function(sender, index)
        healPlayerBig(closestPlayer)
    end

    healSmallItem.Activated = function(sender, index)
        healPlayerSmall(closestPlayer)
    end

    mainMenu:Visible(true)
    _menuPool:MouseEdgeEnabled(false)
end

function ShowNotification(text)
    SetNotificationTextEntry('STRING')
    AddTextComponentString(text)
    DrawNotification(false, true)
end

function revivePlayer(closestPlayer)
	isBusy = true

	ESX.TriggerServerCallback('WSRevive:getItemAmount', function(quantity)
		if quantity > 0 then

            local closestPlayer, closestDistance = ESX.Game.GetClosestPlayer()
			local closestPlayerPed = GetPlayerPed(closestPlayer)

            if closestPlayer == -1 or closestDistance > 3.0 then
                ShowNotification(Translation[config.Locale]['no_players'])
            else

			if IsPedDeadOrDying(closestPlayerPed, 1) then
				local playerPed = PlayerPedId()
				local lib, anim = 'mini@cpr@char_a@cpr_str', 'cpr_pumpchest'
                ShowNotification(Translation[config.Locale]['revive_inprogress'])

				for i=1, 15 do
					Citizen.Wait(900)

					ESX.Streaming.RequestAnimDict(lib, function()
						TaskPlayAnim(playerPed, lib, anim, 8.0, -8.0, -1, 0, 0.0, false, false, false)
					end)
				end

				TriggerServerEvent('WSRevive:removeItem', config.reviveItem)
				TriggerServerEvent('WSRevive:revive', GetPlayerServerId(closestPlayer))
			else
                ShowNotification(Translation[config.Locale]['player_not_unconscious'])
			end
        end
		else
            ShowNotification(Translation[config.Locale]['not_enough_medikit'])
		end
		isBusy = false
	end, config.reviveItem)
end

function healPlayerSmall(closestPlayer)
    isBusy = true

    ESX.TriggerServerCallback('WSRevive:getItemAmount', function(quantity)
    if quantity > 0 then
        local closestPlayer, closestDistance = ESX.Game.GetClosestPlayer()
        local closestPlayerPed = GetPlayerPed(closestPlayer)
        local health = GetEntityHealth(closestPlayerPed)

        if closestPlayer == -1 or closestDistance > 3.0 then
            ShowNotification(Translation[config.Locale]['no_players'])
        else

        if health > 0 then
            local playerPed = PlayerPedId()

            isBusy = true
		    ShowNotification(Translation[config.Locale]['heal_inprogress'])
            TaskStartScenarioInPlace(playerPed, 'CODE_HUMAN_MEDIC_TEND_TO_DEAD', 0, true)
            Citizen.Wait(10000)
            ClearPedTasks(playerPed)

            TriggerServerEvent('WSRevive:removeItem', config.healItemSmall)
            TriggerServerEvent('WSRevive:heal', GetPlayerServerId(closestPlayer), 'small')
            ShowNotification(Translation[config.Locale]['heal_complete'])
            isBusy = false
        else
		    ShowNotification(Translation[config.Locale]['player_not_conscious'])
        end
    end
    else
		ShowNotification(Translation[config.Locale]['not_enough_bandage'])
    end
end, config.healItemSmall)
end

function healPlayerBig(closestPlayer)
	isBusy = true

    ESX.TriggerServerCallback('WSRevive:getItemAmount', function(quantity)
        if quantity > 0 then
            local closestPlayer, closestDistance = ESX.Game.GetClosestPlayer()
            local closestPlayerPed = GetPlayerPed(closestPlayer)
            local health = GetEntityHealth(closestPlayerPed)

            if closestPlayer == -1 or closestDistance > 3.0 then
                ShowNotification(Translation[config.Locale]['no_players'])
            else

            if health > 0 then
                local playerPed = PlayerPedId()

                isBusy = true
		        ShowNotification(Translation[config.Locale]['heal_inprogress'])
                TaskStartScenarioInPlace(playerPed, 'CODE_HUMAN_MEDIC_TEND_TO_DEAD', 0, true)
                Citizen.Wait(10000)
                ClearPedTasks(playerPed)

                TriggerServerEvent('WSRevive:removeItem', config.healItemBig)
                TriggerServerEvent('WSRevive:heal', GetPlayerServerId(closestPlayer), 'big')
		        ShowNotification(Translation[config.Locale]['heal_complete'])
                isBusy = false
            else
		        ShowNotification(Translation[config.Locale]['player_not_conscious'])
            end
        end
        else
		    ShowNotification(Translation[config.Locale]['not_enough_medikit'])
        end
    end, config.healItemBig)
end

function onPlayerDeath()
    isDead = true
    TriggerServerEvent('WSRevive:setDeathStatus', true)
end

AddEventHandler('esx:onPlayerDeath', function(date)
    onPlayerDeath()
end)

RegisterNetEvent('WSRevive:revive')
AddEventHandler('WSRevive:revive', function()
    local playerPed = PlayerPedId()
    local coords = GetEntityCoords(playerPed)
    TriggerServerEvent('WSRevive:setDeathStatus', false)
    
    local formattedCoords = {
        x = ESX.Math.Round(coords.x, 1),
        y = ESX.Math.Round(coords.y, 1),
        z = ESX.Math.Round(coords.z, 1)
    }

    RespawnPed(playerPed, formattedCoords, 0.0)
    StopScreenEffect('DeathFailOut')
end)

function RespawnPed(ped, coords, heading)
	SetEntityCoordsNoOffset(ped, coords.x, coords.y, coords.z, false, false, false, true)
	NetworkResurrectLocalPlayer(coords.x, coords.y, coords.z, heading, true, false)
	SetPlayerInvincible(ped, false)
	ClearPedBloodDamage(ped)

	TriggerServerEvent('esx:onPlayerSpawn')
	TriggerEvent('esx:onPlayerSpawn')
	TriggerEvent('playerSpawned')
end

RegisterNetEvent('WSRevive:heal')
AddEventHandler('WSRevive:heal', function(healType, quiet)
	local playerPed = PlayerPedId()
	local maxHealth = GetEntityMaxHealth(playerPed)

	if healType == 'small' then
		local health = GetEntityHealth(playerPed)
		local newHealth = math.min(maxHealth, math.floor(health + maxHealth / 8))
		SetEntityHealth(playerPed, newHealth)
	elseif healType == 'big' then
		SetEntityHealth(playerPed, maxHealth)
	end

	if not quiet then
		ShowNotification(Translation[config.Locale]['healed'])
	end
end) 

Lua online compiler

Write, Run & Share Lua code online using OneCompiler's Lua online compiler for free. It's one of the robust, feature-rich online compilers for Lua language, running the latest Lua version 5.4. Getting started with the OneCompiler's Lua editor is easy and fast. The editor shows sample boilerplate code when you choose language as Lua and start coding.

Taking inputs (stdin)

OneCompiler's Lua online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Lua program which takes name as input and prints hello message with your name.

name = io.read("*a")
print ("Hello ", name)

About Lua

Lua is a light weight embeddable scripting language which is built on top of C. It is used in almost all kind of applications like games, web applications, mobile applications, image processing etc. It's a very powerful, fast, easy to learn, open-source scripting language.

Syntax help

Variables

  • By default all the variables declared are global variables
  • If the variables are explicitly mentioned as local then they are local variables.
  • Lua is a dynamically typed language and hence only the values will have types not the variables.

Examples

-- global variables
a = 10

-- local variables

local x = 30
Value TypeDescription
numberRepresents numbers
stringRepresents text
nilDifferentiates values whether it has data or not
booleanValue can be either true or false
functionRepresents a sub-routine
userdataRepresents arbitary C data
threadRepresents independent threads of execution.
tableCan hold any value except nil

Loops

1. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition)
do
--code
end

2. Repeat-Until:

Repeat-Until is also used to iterate a set of statements based on a condition. It is very similar to Do-While, it is mostly used when you need to execute the statements atleast once.

repeat
   --code
until( condition )

3. For:

For loop is used to iterate a set of statements based on a condition.

for init,max/min value, increment
do
   --code
end

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increase re-usuability and modularity.

optional_function_scope function function_name( argument1, argument2, argument3........, argumentn)
--code
return params with comma seperated
end