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
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
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.