Combined Engine Toggle and Leave Engine Running resource for FiveM.
Intro
Hi, this is my first sort-of script, so if there’s any suggestions to optimize the code or any features you’d like added, please feel free to let me know!
It’s meant for more serious RP servers.
I wanted to find a resource for my server that would force the player to use their scroll wheel to toggle the engine on and off to use vehicles, as well as leaving the engine running if they got out of the car with it running. I searched all over the forums for such a script and couldn’t find it, so I made one myself.
Resource
The resource limits the player to the following:
Cannot start engine with ‘W’ key.
Disables auto-start when a player enters a vehicle or when vehicle is turned off.
Toggle engine state with Scrollwheel Up only (editable in client.lua).
Engine stays on after exiting the vehicle.
Due to how basic the script is, if you get into and out of the vehicle too fast the engine will turn off, I will look into this over the next few days to see what I can do about it.
Hey. Good release. I took a quick glance at the code.
Some notes.
You can replace GetPlayerPed() with PlayerPedId() as it runs faster.
You can optimize the script by checking if the player is in a car, and if not then Wait(1000), or another number that’d work for the script. You could put your vehicle variable in that statement as well since you don’t have to loop through it when it’s not being used.
You can move local engineStatus outside of the loop.
You could do the function in this manner, and take use of the variables in the loop. function toggleEngine(vehicle)
function toggleEngine(vehicle)
if vehicle ~= nil and vehicle ~= 0 then
local engineRunning = GetIsVehicleEngineRunning(vehicle)
if engineRunning then
SetVehicleEngineOn(vehicle, false, true, true)
else
SetVehicleEngineOn(vehicle, true, true, true)
end
end
end
Can Someone help I don’t understend why my code dosen’t work the gui works fine but when I check the checkbox and leave the vehicle the car shutsdown like normaly
Citizen.CreateThread(function()
while True do
Citizen.Wait(0)
if engine == true then
if GetVehiclePedIsIn(PlayerPedId(),false) ~= nil and GetVehiclePedIsIn(PlayerPedId(),false) ~=0 then
local engineRunning = GetIsVehicleEngineRunning(GetVehiclePedIsIn(PlayerPedId(),false))
if engineRunning then
SetVehicleEngineOn(GetVehiclePedIsIN(PlayerPedId(),false),false,true,true)
else
SetVehicleEngineOn(GetVehiclePedIsIn(PlayerPedId(),false),true,true,true)
end
end
end
end
end)
RageUI.Checkbox("Keep Engine on", "Always Keeps the engine on", engine, {}, function(Hovered, Active, Selected, Checked)
if Selected then
engine = Checked
end
end)