I've found a bug in a native function

Citizen.CreateThread(function ()
    local model = "mp_m_freemode_01"
    local playerId = PlayerId()

    RequestModel(model)

    while not HasModelLoaded(model) do Citizen.Wait(0) end

    SetPlayerModel(playerId, model)
    SetPedDefaultComponentVariation(PlayerPedId())
    SetModelAsNoLongerNeeded(model)
end)

This code above works but this one below doesn’t

Citizen.CreateThread(function ()
    local model = "mp_m_freemode_01"
    local playerId = PlayerId()
    local pedId = PlayerPedId()

    RequestModel(model)

    while not HasModelLoaded(model) do Citizen.Wait(0) end

    SetPlayerModel(playerId, model)
    SetPedDefaultComponentVariation(pedId)
    SetModelAsNoLongerNeeded(model)
end)

Why?

Becuase PlayerPedId() returns the current ped handle, when you use SetPlayerModel() the ped handle changes, meaning the handle you have stored in pedId is ‘out of date’. Either define pedId after calling SetPlayerModel(), or just inline PlayerPedId().

1 Like

Thanks :slight_smile: