local loadingScreenFinished = false
local ready = false
local isInCreator = false
local creatorCam = nil
local lastData = {}
local lastSpawnpoint = nil

RegisterNUICallback("quit", function(data, cb)
    cb(true)

    TriggerServerEvent("cataleya_charcreator:quit")
end)

RegisterNUICallback("register", function(data, cb)
    TriggerServerEvent("esx_identity:registerIdentity", data)

    ESX.TriggerServerCallback('esx_identity:registerIdentity', function(callback)
        if not callback then
            return
        end

        SendNUIMessage({
            eventName = "setUIVisibility",
            visible = true,
            stage = 2
        })
    end, data)

    cb(true)
end)

RegisterNUICallback("rotate", function(data, cb)
    SetEntityHeading(PlayerPedId(), (Config.Position.w + data.value) + 0.0)

    cb(true)
end)

RegisterNUICallback("finish", function(data, cb)
    SendNUIMessage({
        eventName = "setUIVisibility",
        visible = false
    })

    SwitchOutPlayer(PlayerPedId(), 0, 1)

    while (GetPlayerSwitchState() ~= 5) do
        Wait(0)
    end

    Wait(255)
    if (not lastSpawnpoint) then
        SetFocusPosAndVel(Config.SpawnPoint.x, Config.SpawnPoint.y, Config.SpawnPoint.z, 0.0, 0.0, 0.0)
    else
        SetFocusPosAndVel(lastSpawnpoint.x, lastSpawnpoint.y, lastSpawnpoint.z, 0.0, 0.0, 0.0)
    end

    Wait(50)

    if (not lastSpawnpoint) then
        local playerPed = PlayerPedId()
        FreezeEntityPosition(playerPed, true)
        SetEntityCoordsNoOffset(playerPed, Config.SpawnPoint.x, Config.SpawnPoint.y, Config.SpawnPoint.z, false, false,
            false)
        SetEntityHeading(playerPed, Config.SpawnPoint.w)
    else
        local playerPed = PlayerPedId()
        FreezeEntityPosition(playerPed, true)
        SetEntityCoordsNoOffset(playerPed, lastSpawnpoint.x, lastSpawnpoint.y, lastSpawnpoint.z, false, false,
            false)
        SetEntityHeading(playerPed, 0.0)
    end

    Wait(500)

    SetNuiFocus(false, false)
    if (creatorCam) then
        DestroyCam(creatorCam, true)
    end
    RenderScriptCams(false, false, 0, true, true)
    TriggerServerEvent("cataleya_charcreator:ResetPlayerRoutingBucket")

    TriggerEvent('skinchanger:getSkin', function(skin)
        TriggerServerEvent('esx_skin:save', skin)
    end)

    Wait(200)
    FreezeEntityPosition(PlayerPedId(), false)

    isInCreator = false
    Config.ShowHUD()
    TriggerEvent("isCreatingCharacter", false)

    SwitchInPlayer(PlayerPedId())

    TriggerEvent("c_einreise:openUI")
    lastSpawnpoint = nil

    cb(true)
end)

function ConvertSkinchanger(data)
    local t = {}

    for key, value in pairs(data) do
        t[value.name] = value
    end

    return t
end

RegisterNUICallback("selectGender", function(data, cb)
    local components = json.decode(json.encode(GetDefaultComponents()))

    components.sex = data.gender

    TriggerEvent('skinchanger:loadSkin', components)

    UpdateComponents()

    cb(true)
end)

RegisterNUICallback("updateValue", function(data, cb)
    TriggerEvent('skinchanger:change', data.name, tonumber(data.value))

    if (not creatorCam) then
        return cb(true)
    end

    local dataComponent = lastData[data.name]

    local heading = GetEntityHeading(PlayerPedId())

    SetEntityHeading(PlayerPedId(), Config.Position.w + 0.0)
    local offsetPos = GetOffsetFromEntityInWorldCoords(PlayerPedId(), 0.0, dataComponent.zoomOffset,
        dataComponent.camOffset)
    SetEntityHeading(PlayerPedId(), heading)
    local camPos = vector3(offsetPos.x, offsetPos.y, offsetPos.z)
    local camRotation = GetCamRot(creatorCam, 0)

    local newCam = CreateCamFunc(camPos, camRotation, GetCamFov(creatorCam), false)
    SetCamActiveWithInterp(newCam, creatorCam, 3000, 1, 1)
    DestroyCam(creatorCam, true)
    creatorCam = newCam

    cb(true)
end)

function UpdateComponents()
    local components    = json.decode(json.encode(GetDefaultComponents()))
    local maxComponents = nil
    local result        = {}

    TriggerEvent("skinchanger:getData", function(data, max)
        lastData = ConvertSkinchanger(data)
        maxComponents = max
    end)

    while (not maxComponents) do
        Wait(0)
    end

    for key, value in pairs(Config.Components) do
        if (value.category) then
            value.max = maxComponents[value.name]

            table.insert(result, value)
        end
    end

    SendNUIMessage({
        eventName = "updateComponents",
        components = result
    })
end

RegisterNUICallback("zoom", function(data, cb)
    if (not creatorCam) then
        return cb(true)
    end
    local newFov = GetCamFov(creatorCam)

    if (data.direction == "in") then
        newFov = newFov - Config.CamZoomAmount
    else
        newFov = newFov + Config.CamZoomAmount
    end

    newFov = math.max(Config.CamMinFov, math.min(Config.CamMaxFov, newFov)) + 0.0

    SetCamFov(creatorCam, newFov)

    cb(true)
end)

CreateThread(function()
    if (ESX.IsPlayerLoaded()) then
        loadingScreenFinished = true
    end
end)

RegisterNetEvent('esx_identity:alreadyRegistered', function()
    while not loadingScreenFinished do Wait(100) end
    TriggerEvent('esx_skin:playerRegistered')
end)

RegisterNetEvent('esx_identity:setPlayerData', function(data)
    SetTimeout(1, function()
        ESX.SetPlayerData("name", ('%s %s'):format(data.firstName, data.lastName))
        ESX.SetPlayerData('firstName', data.firstName)
        ESX.SetPlayerData('lastName', data.lastName)
        ESX.SetPlayerData('dateofbirth', data.dateOfBirth)
        ESX.SetPlayerData('sex', data.sex)
        ESX.SetPlayerData('height', data.height)
    end)
end)

AddEventHandler('esx:loadingScreenOff', function()
    loadingScreenFinished = true
end)

RegisterNUICallback('ready', function(data, cb)
    ready = true
    cb(true)
end)

RegisterNetEvent('esx_identity:showRegisterIdentity', function(stage, otherStuff)
    if (stage or otherStuff) then
        lastSpawnpoint = GetEntityCoords(PlayerPedId())
    end

    isInCreator = true
    Config.HideHUD()
    TriggerEvent("isCreatingCharacter", true)

    TriggerServerEvent("cataleya_charcreator:SetPlayerRoutingBucket")

    DoScreenFadeOut(0)
    SetFocusPosAndVel(Config.Position.x, Config.Position.y, Config.Position.z, 0.0, 0.0, 0.0)

    TriggerEvent('esx_skin:resetFirstSpawn')

    while not (ready and loadingScreenFinished) do
        Wait(0)
    end

    Wait(50)

    CreateThread(function()
        while (isInCreator) do
            ForcePedMotionState(PlayerPedId(), 0x9072A713, true, 1, false)
            Wait(0)
        end
    end)

    RenderScriptCams(false, false, 0, false, false)
    DestroyAllCams(true)
    DestroyAllCams(false)

    local playerPed = PlayerPedId()
    FreezeEntityPosition(playerPed, true)
    SetEntityCoordsNoOffset(playerPed, Config.Position.x, Config.Position.y, Config.Position.z, false, false, false)
    SetEntityHeading(playerPed, Config.Position.w)

    local offsetPos = GetOffsetFromEntityInWorldCoords(PlayerPedId(), 0.0, 1.5, 0.0)
    local bonePos = GetPedBoneCoords(PlayerPedId(), 12844, 0, 0, 0)
    local camPos = vector3(offsetPos.x, offsetPos.y, bonePos.z)
    local rx = GetEntityRotation(PlayerPedId(), 2)
    local camRotation = rx + vector3(0.0, 0.0, 180)

    creatorCam = CreateCamFunc(camPos, camRotation, 100.0, true)
    RenderScriptCams(true, false, 0, false, false)

    TriggerEvent('skinchanger:loadSkin', GetDefaultComponents())

    Wait(500)

    SetNuiFocus(true, true)
    DoScreenFadeIn(250)
    SendNUIMessage({
        eventName = "setUIVisibility",
        visible = true,
        settings = Config.IdentitySettings,
        discordLink = Config.DiscordLink,
        description = Config.Description,
        stage = stage or nil,
        blockBack = stage ~= nil
    })
end)

RegisterCommand("debugCreator", function()
    TriggerEvent('esx_identity:showRegisterIdentity')
end, false)

function CreateCamFunc(position, rotation, fov, active)
    local cam = CreateCam("DEFAULT_SCRIPTED_CAMERA", active)
    SetCamCoord(cam, position.x, position.y, position.z)
    SetCamFov(cam, fov)
    SetCamRot(cam, rotation.x, rotation.y, rotation.z, 0)

    return cam
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