[FREE] CarLock - Animation & Sound

:bookmark_tabs: Description

This is a simple standalone Car Lock script made for FiveM.

  • DELETE - Save/unsave the vehicle you are in
  • U - Lock/unlock the saved car

The keys can be changed in the game menu.

:bulb: Features

  • Simple and standalone
  • Key fob animation
  • Sound effect
  • Flashing car lights
  • Configurable keys & interaction radius

:eyes: Preview

:bar_chart: Resmon

Context CPU
Idle 0.00 ms
Peak 0.01 ms

:inbox_tray: Installation

  • Rename the folder from CarLock-main to CarLock
  • Drag the folder to your server resource folder
  • Add start CarLock or ensure CarLock to your server.cfg

:white_check_mark: Changelog

v1.2

  • Added version checker

v1.1

  • Added keybind option
  • Minor tweaks
    (Thanks to cyanURP and Geerdodagr8)

v1.0

  • Initial Release

:arrow_down: Download

9 Likes

add esx integration

Very nice script! By the way, could you add a function for pressing a key too? Instead of typing it, it could save time when let’s say you get a call and you are in a hurry.

1 Like

Currently not in my plans, sorry. But if there’s a more experienced scripter out there willing to make this esx, they surely can

Thanks. The original idea was to make it a button, but it wasn’t working, since I literally just started with lua, and I still can’t figure out how to make it work.

But I’m still planning to make it on keypress for sure, maybe if there is someone out there willing to help me with that, it’d would be great.

Please fix the deprecated chatMessage commands in server.lua see the docs for more info.

-- CONFIG 
lockDistance = 50 -- The radius you have to be in to lock/unlock your vehicle.

--[[
─────────────────────────────────────────────────────────────────

	CarLock (client.lua) - Created by ItzEndah
	Current Version: 1.0 (July 2021)
	
	Support - ItzEndah#0001 on Discord
	
	DO NOT EDIT BELOW IF YOU DON'T KNOW WHAT YOU ARE DOING	

─────────────────────────────────────────────────────────────────
]]--

saved = false

-- Request animation
Citizen.CreateThread(function()
    local dict = "anim@mp_player_intmenu@key_fob@"
	RequestAnimDict(dict)
    while not HasAnimDictLoaded(dict) do
    Citizen.Wait(0)
end
	
-- Lock lights event
function lockLights()
local vehicle = saveVehicle
	SetVehicleLights(vehicle, 2)
	Wait (200)
	SetVehicleLights(vehicle, 0)
	Wait (200)
	SetVehicleLights(vehicle, 2)
	Wait (400)
	SetVehicleLights(vehicle, 0)
end)

RegisterKeyMapping('+vehiclelock', 'Vehicle Lock', 'keyboard', 'l')

-- Lock vehicle
RegisterCommand('+vehiclelock', function()
	local player = GetPlayerPed(-1)
    local vehicle = saveVehicle
	local isLocked = GetVehicleDoorLockStatus(vehicle)
	local distanceToVeh = GetDistanceBetweenCoords(GetEntityCoords(player), GetEntityCoords(vehicle), 1)
		if DoesEntityExist(vehicle) then
			if distanceToVeh <= lockDistance then
				if (isLocked == 1) then
				PlaySoundFrontend(-1, "BUTTON", "MP_PROPERTIES_ELEVATOR_DOORS", 1)
				TaskPlayAnim(GetPlayerPed(-1), dict, "fob_click_fp", 8.0, 8.0, -1, 48, 1, false, false, false)
				SetVehicleDoorsLocked(vehicle, 2)
				ShowNotification("You have ~r~locked~r~ ~w~your vehicle.")
				lockLights()
				else
				PlaySoundFrontend(-1, "BUTTON", "MP_PROPERTIES_ELEVATOR_DOORS", 1)
				TaskPlayAnim(GetPlayerPed(-1), dict, "fob_click_fp", 8.0, 8.0, -1, 48, 1, false, false, false)
				SetVehicleDoorsLocked(vehicle,1)
				ShowNotification("You have ~g~unlocked~g~ ~w~your vehicle.")
				lockLights()
				end
			else
				ShowNotification("~r~You must be closer to your vehicle.")
			end
		else
			ShowNotification("~r~No saved vehicle.")
		end
	end)
end)

RegisterKeyMapping('+vehiclelocksave', 'Save Vehicle Lock', 'keyboard', 'delete')

-- Save vehicle
RegisterCommand('+vehiclelocksave', function()
	local player = GetPlayerPed(-1)
	if (IsPedSittingInAnyVehicle(player)) then 
		if saved == true then
			saveVehicle = nil
			RemoveBlip(targetBlip)
			ShowNotification("Saved vehicle ~r~removed~w~.")
			saved = false
		else
			RemoveBlip(targetBlip)
			saveVehicle = GetVehiclePedIsIn(player,true)
			local vehicle = saveVehicle
			targetBlip = AddBlipForEntity(vehicle)
			SetBlipSprite(targetBlip,225)
			ShowNotification("Vehicle ~g~saved~w~.")
			saved = true
		end
	end
end)

-- Notification function
function ShowNotification(text)
    SetNotificationTextEntry("STRING")
    AddTextComponentString(text)
    DrawNotification(false, false)
end

Something like this?

1 Like

Working on it!

I have made a pull request and have added a keybind option, in the future i suggest using PlayerPedId() instead off GetPlayerPed(-1) and i suggest doing the math like i did in the pr instead of using GetDistanceBetweenCoords

Alright, appreciate it!

Key press has been added :slight_smile:

Thanks mate! Btw the locking lights looks good but not on addon vehicles? They glitch kind of.
Also, could you add sound when locking? Like using your own sounds would be good. Streams a sound every time you lock/unlock it. Would be very cool! Nice work man.

Thank you!

What do you mean by glitch off? I’m using it on my server which has tons of addon cars and never had a problem with the lights, as you can also see by the demo.

Regarding the sound, I’ll see what I can do :slight_smile:

The locking lights blink very fast in a weird way and not like on the replace vehicles. Maybe it’s because I have emergency lighting system on my car :thinking:

I’m not really sure, but that could be it.

Maybe try to play a little with the Wait( ) instructions in the client.lua: the higher the number, the more time the lights will stay on when they flash.

client

Cheers! I will try that out.

So I edited it, and now it’s working thanks mate. Btw ping me when the sound function is out, I am very looking forward to that! : D

No sound for me either.

There is actually but it’s not that loud, because its just a click that simulates the button press. You can hear it if you listen close on the demo video.

I haven’t been able to find a better audio that is already in the game, sadly.
But as someone suggested, I will try to make a custom one.

People can literally just use InteractSound when the events happen and do it on their own.

1 Like