> For the complete documentation index, see [llms.txt](https://docs.ak4y.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ak4y.com/scripts/ak4y-multicharacter-v3/editable-files/cl_utils.lua.md).

# cl\_utils.lua

```lua
CORE = exports["ak4y-core"]
Framework = nil
loadEvent = "QBCore:Client:OnPlayerLoaded"

CreateThread(function()
    if not Framework then
        if GetResourceState("qbx_core") == "started" then
            Framework = "qb"
        elseif GetResourceState("es_extended") == "started" then
            Framework = "esx"
        elseif GetResourceState("qb-core") == "started" then
            Framework = "qb"
        end
    end
end)

CreateThread(function()
    if Framework == "qb" then
        loadEvent = "QBCore:Client:OnPlayerLoaded"
    elseif Framework == "esx" then
        loadEvent = 'esx:playerLoaded'
    elseif Framework == "oldEsx" then
        loadEvent = 'esx:playerLoaded'
    end

    RegisterNetEvent(loadEvent, function()

    end)
end)

if Framework == "esx" then
    RegisterNetEvent('esx:playerLoaded')
    AddEventHandler('esx:playerLoaded', function(playerData, isNew, skin)
        local spawn = playerData.coords or Config.DefaultSpawn

        if isNew or not skin or (type(skin) == "table" and #skin == 1) then
            if playerData.sex ~= nil then
                if skin and type(skin) == "table" then
                    skin.sex = playerData.sex == "m" and 0 or 1
                end
            end
            local model = (skin and skin.sex == 0) and "mp_m_freemode_01" or "mp_f_freemode_01"
            RequestModel(model)
            SetPlayerModel(PlayerId(), model)
            SetModelAsNoLongerNeeded(model)
            if GetResourceState('skinchanger') == 'started' then
                TriggerEvent('skinchanger:loadSkin', skin, function()
                    local playerPed = PlayerPedId()
                    SetPedAoBlobRendering(playerPed, true)
                    ResetEntityAlpha(playerPed)
                    if GetResourceState('esx_skin') == 'started' then
                        TriggerEvent('esx_skin:openSaveableMenu')
                    end
                end)
            end
        end

        local playerPed = PlayerPedId()
        FreezeEntityPosition(playerPed, true)
        SetEntityCoordsNoOffset(playerPed, spawn.x, spawn.y, spawn.z, false, false, false, true)
        if spawn.heading then
            SetEntityHeading(playerPed, spawn.heading)
        end

        if not isNew and skin and GetResourceState('skinchanger') == 'started' then
            TriggerEvent('skinchanger:loadSkin', skin)
        end

        Config.PlayerLoadedESX(isNew)
        TriggerServerEvent('esx:onPlayerSpawn')
        TriggerEvent('esx:onPlayerSpawn')
        TriggerEvent('playerSpawned')
        TriggerEvent('esx:restoreLoadout')
    end)
end

function ClothingExports(skinData)
    if GetResourceState('rcore_clothing') == 'started' then
        exports.rcore_clothing:setPedSkin(PlayerPedId(), skinData)
    elseif GetResourceState('crm-appearance') == 'started' then
        exports['crm-appearance']:crm_set_ped_appearance(PlayerPedId(), skinData)
    elseif GetResourceState('bl_appearance') == 'started' then
        TriggerEvent('skinchanger:loadSkin', skinData)
    elseif GetResourceState('illenium-appearance') == 'started' then
        exports['illenium-appearance']:setPlayerAppearance(skinData)
    elseif GetResourceState('fivem-appearance') == 'started' then
        exports['fivem-appearance']:setPlayerAppearance(skinData)
    elseif GetResourceState('skinchanger') == 'started' then
        TriggerEvent('skinchanger:loadSkin', skinData)
    else
        print('You can add your own clothing exports here.')
    end
end

function SetPedSkin(cacheped, skinData)
    if GetResourceState('rcore_clothing') == 'started' then
        exports.rcore_clothing:setPedSkin(cacheped, skinData)
    elseif GetResourceState('crm-appearance') == 'started' then
        exports['crm-appearance']:crm_set_ped_appearance(cacheped, skinData)
    elseif GetResourceState('bl_appearance') == 'started' then
        TriggerEvent('skinchanger:loadSkin', skinData)
    elseif GetResourceState('illenium-appearance') == 'started' then
        exports['illenium-appearance']:setPedAppearance(cacheped, skinData)
    elseif GetResourceState('fivem-appearance') == 'started' then
        exports['fivem-appearance']:setPedAppearance(cacheped, skinData)
    elseif GetResourceState('skinchanger') == 'started' then
    else
        print('You can add your own clothing exports here.')
    end
end

function SetSkin(cid)
    local skindata = CORE:Trigger('ak4y-multicharacter-v3:GetSkinData', cid)

    if Framework == "esx" then
        if skindata then
            if type(skindata) == "string" then
                skindata = json.decode(skindata)
            end

            if skindata and skindata.model then
                local modelHash = joaat(skindata.model)
                RequestModel(modelHash)
                while not HasModelLoaded(modelHash) do
                    Wait(50)
                end
                SetPlayerModel(PlayerId(), modelHash)

                SetPedHeadBlendData(PlayerPedId(), 0, 0, 0, 0, 0, 0, 1.0, 1.0, 1.0, false)

                if skindata.model == "mp_m_freemode_01" or skindata.model == "mp_f_freemode_01" then
                    if GetResourceState('skinchanger') == 'started' then
                        TriggerEvent('skinchanger:loadSkin', skindata)
                    end
                end
            else
                -- If no model, check sex and create default model
                if skindata and (skindata.sex == 0 or skindata.sex == 1) then
                    local model = (skindata.sex == 0) and "mp_m_freemode_01" or "mp_f_freemode_01"
                    skindata.model = model
                    local modelHash = joaat(model)
                    RequestModel(modelHash)
                    while not HasModelLoaded(modelHash) do
                        Wait(50)
                    end
                    SetPlayerModel(PlayerId(), modelHash)

                    SetPedHeadBlendData(PlayerPedId(), 0, 0, 0, 0, 0, 0, 1.0, 1.0, 1.0, false)

                    if GetResourceState('skinchanger') == 'started' then
                        TriggerEvent('skinchanger:loadSkin', skindata)
                    end
                else
                    print("^3[WARN]^7 No skin data or sex info for character: " .. tostring(cid))
                end
            end
        else
            print("^1[ERROR]^7 Skin data not found for character: " .. tostring(cid))
        end
    -- QB skin loading
    elseif Framework == "qb" then
        skindata = skindata[1]
        if skindata then
            if skindata.model then
                local modelHash = joaat(skindata.model)
                RequestModel(modelHash)
                while not HasModelLoaded(modelHash) do
                    Wait(50)
                end
                SetPlayerModel(PlayerId(), modelHash)

                SetPedHeadBlendData(PlayerPedId(), 0, 0, 0, 0, 0, 0, 1.0, 1.0, 1.0, false)

                if skindata.model == "mp_m_freemode_01" or skindata.model == "mp_f_freemode_01" then
                    if skindata.skin then
                        exports['illenium-appearance']:setPlayerAppearance(json.decode(skindata.skin))
                    end
                end
            end
        else
            print("Skin data not found: " .. tostring(cid))
        end
    end
end
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.ak4y.com/scripts/ak4y-multicharacter-v3/editable-files/cl_utils.lua.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
