(RESOLVED) - Why it doesn't work?

Hi guys,

I tried to convert the LockDoors script to close and open the cars without going through the /lock but by the G key.
Something is wrong, here is my script

car = false
lastCar = nil

    while true do
        if IsControlJustPressed(1, 47) then
            Citizen.Trace("Control Was Pressed")

            car = GetVehiclePedIsIn(GetPlayerPed(-1), false)

            if not car and lastCar == nil then
                TriggerEvent("chatMessage", "ERROR", {255, 0, 0}, "You have to sit in a vehicle to set it as yours.")
            elseif car then
                lastCar = car

            lockStatus = GetVehicleDoorLockStatus(lastCar)
            if lockStatus == 0 or lockStatus == 1 then
                Citizen.Trace("DOORS ARE LOCKED")
                SetVehicleDoorsLocked(lastCar, 2)
                SetVehicleDoorsLockedForPlayer(lastCar, PlayerId(), false)
                TriggerEvent("chatMessage", "INFO", {255, 255, 0}, "Door is now ^1locked^0.")
                Citizen.Trace("DOORS ARE LOCKED")
                SetVehicleDoorsLocked(lastCar, 1)
                TriggerEvent("chatMessage", "INFO", {255, 255, 0}, "Door is now ^2unlocked^0.")

The problem is that the script displays directly Door is now Locked.
A solution ? I am blocked … :frowning:

Can you tell me what you did to get this too work?

Thanks ahead of time.

I started everything again to have a clean base and I realized that the variables car and lastCar was never nil so I think it’s due to it.

Otherwise here is the script I did, it also checks that the player is within 5 meters of the car to be able to close it and open it. The key is U.


I do not try it in multiplayer but it should work. Keep me informed :slight_smile:

It seems to be working :slight_smile:

Thanks again for the code, greatly appreciated!!

Can you refresh link?)


But I do not work on it right now and there are people who have bugs with it.

i need system without keys .I need press button and car open and press button car lock and its all what i need.