-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoLock.lua
65 lines (61 loc) · 2.21 KB
/
AutoLock.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
script_name("AutoLock")
script_description("Auto lock vehicle")
-- Automatically locks your vehicle, and automatically
-- opens locked vehicles when you press F or ENTER
script_url("https://github.com/ins1x/moonloader-scripts")
script_version("0.5")
script_author("1NS")
-- encoding.default = 'CP1251'
-- script_moonloader(16) moonloader v.0.26
-- activation: auto
-- Will work on vehicles within a radius of 3 meters
local maxSearchVehRadius = 3
local recallDelay = 1000
function main()
if not isSampLoaded() or not isSampfuncsLoaded() then return end
while not isSampAvailable() do wait(100) end
while true do
wait(0)
if isKeyJustPressed(0x0D) or isKeyJustPressed(0x46) then -- Enter/F
if not sampIsChatInputActive() and not sampIsDialogActive() then
if isCharInAnyCar(playerPed) then
local currentcarhandle = storeCarCharIsInNoSave(playerPed)
local currentcarDoorStatus = getCarDoorLockStatus(currentcarhandle)
if currentcarDoorStatus ~= 2 then -- isNotPassenger check
wait(recallDelay)
sampSendChat("/lock")
end
else
local closestcarhandle, closestcarid = getClosestCar(maxSearchVehRadius)
if closestcarhandle then
wait(recallDelay)
sampSendChat("/lock")
end
end
end
end
-- L key bind to /lock vehicle
--if isKeyJustPressed(76) and not sampIsChatInputActive() and not sampIsDialogActive() then
--sampSendChat("/lock")
--end
end
end
function getClosestCar(minDist)
-- TIP: return 2 values: car handle and car id
local closestId = -1
local closestHandle = false
local x, y, z = getCharCoordinates(playerPed)
for i, k in ipairs(getAllVehicles()) do
local streamed, carId = sampGetVehicleIdByCarHandle(k)
if streamed then
local xi, yi, zi = getCarCoordinates(k)
local dist = math.sqrt( (xi - x) ^ 2 + (yi - y) ^ 2 + (zi - z) ^ 2 )
if dist < minDist then
minDist = dist
closestId = carId
closestHandle = k
end
end
end
return closestHandle, closestId
end