Unlock/Lock function help!

Hello im new to FiveM development but sat down today and worked on a easy unlock/lock script, it’s basic and shouldn’t require that much experience to make. However while making it I ran into a problem where it says the client native of GetVehicleLockStatus() Should return 1/2 for unlocked/locked. However it seems like it still operates like the server side of the native and sends 0 both for locked and unlocked causing the command to only work once and then continue to lock the car.

FxManifest

fx_version 'cerulean'
game 'gta5'
lua54 'yes'

client_script {
    'client.lua'
}

Client.lua

local player = PlayerPedId()
local playerVehicle = GetPlayersLastVehicle(player)

RegisterCommand('+lock', function(source)
   local lockStatus = 	GetVehicleDoorLockStatus(playerVehicle)

   if lockStatus == 1 then
    SetVehicleDoorsLocked(playerVehicle, 2)
    TriggerEvent('chat:addMessage', {
        color = { 255, 0, 0},
        multiline = true,
        args = {"Your car is now locked!"}
      })
   elseif lockStatus == 2 then
   SetVehicleDoorsLocked(pVehicle, 1)
   TriggerEvent('chat:addMessage', {
    color = { 255, 0, 0},
    multiline = true,
    args = {"Your car is now unlocked!"}
  })
else
   end
end)

RegisterKeyMapping('+lock', 'Lock your car!', 'keyboard', 'o')

This is the new version the older one was lockStatus == 0

1 Like

Hey Try This Snippet It May Improve your Result

local player = PlayerPedId()
Command to lock/unlock the player’s vehicle
RegisterCommand(‘+lock’, function()
Get the player’s last vehicle
local playerVehicle = GetVehiclePedIsIn(player, false)

Check if the player is in a valid vehicle
if playerVehicle and DoesEntityExist(playerVehicle) then
Get the current lock status of the vehicle
local lockStatus = GetVehicleDoorLockStatus(playerVehicle)

    if lockStatus == 1 then -- Vehicle is unlocked
        SetVehicleDoorsLocked(playerVehicle, 2) -- Lock the vehicle
        notifyPlayer("Your car is now locked!")
    elseif lockStatus == 2 then -- Vehicle is locked
        SetVehicleDoorsLocked(playerVehicle, 1) -- Unlock the vehicle
        notifyPlayer("Your car is now unlocked!")
    else
        notifyPlayer("Unable to determine lock status!")
    end
else
    notifyPlayer("No valid vehicle detected!")
end

end, false)

Helper function to send chat messages to the player
function notifyPlayer(message)
TriggerEvent(‘chat:addMessage’, {
color = { 255, 0, 0 },
multiline = true,
args = { message }
})
end

Both 0 and 1 operate the same way on client side, however the server does sync 1 as 0.

1 Like

Mate you’re never refreshing the players car, you get it once at runtime start and then never again. The native will always fail when passed an invalid entity.

1 Like

Ahh see what you mean thanks man

Thanks after a little bit of tweaking this worked, appreciate it!

no problem