First of all, this is my first post so please be gentle I am by no means a scripter in any right, but I do know how to do basic things from python modding days. Bear with me.
I have been messing with esx cruise control for a few weeks trying to build onto the existing framework some basic features, using mostly copy and paste methods, and adjusting the global variables as needed; incremental accel and decel, removal of boats, bikes, and helis from cruising, and a top speed limiter to preserve the vehicle’s “estimated” top speed + 4.5 (seems to be about right for all vehicles, within 1-2 mph high and low). The problem is… that there is no problem… THE CODE I have performs magnificently, however the one small hiccup which shows up in F8 debug:
[ 91984] [b2545_GTAProce] MainThrd/ ^1SCRIPT ERROR: @esx_cruisecontrol/client/main.lua:87: attempt to compare boolean with number^7
[ 91984] [b2545_GTAProce] MainThrd/ ^3> fn^7 (^5@esx_cruisecontrol/client/main.lua^7:87)
The line it’s saying is an error, I had to create the equation here (line 87) for the top speed limiter. It compares Entity speed vs Vehicle estimated max speed. If I remove the line, or otherwise alter it to function differently, it causes the vehicle to abruptly halt upon cruise disengage, but ONLY AFTER using the Accel feature, the Decel feature did not need the top speed check. WITH the line added (which debug says is an error), ALL features run flawless and cruise control disengages without any abrupt stopping. Below is my esx_cruisecontrol\client\main.lua
local CruisedSpeed, AccelSpeed, DecelSpeed, TopSpeed, CruisedSpeedKm, VehicleVectorY = 0, 0, 0
RegisterCommand("cruise", function(src)
if IsDriver() and not IsDrivingBannedVeh() then
TriggerCruiseControl()
end
end)
RegisterCommand("increase", function(src)
if IsDriver() and not IsDrivingBannedVeh() then
TriggerAccel()
end
end)
RegisterCommand("decrease", function(src)
if IsDriver() and not IsDrivingBannedVeh() then
TriggerDecel()
end
end)
RegisterKeyMapping("cruise", "Cruise Set", "keyboard",Config.ToggleKey)
RegisterKeyMapping("increase", "Cruise Accel", "keyboard",Config.ToggleKey1)
RegisterKeyMapping("decrease", "Cruise Decel", "keyboard",Config.ToggleKey2)
function TriggerCruiseControl()
if CruisedSpeed == 0 and IsDriving() then
if GetVehicleSpeed() > 0 and GetVehicleCurrentGear(GetVehicle()) > 0 then
CruisedSpeed = GetVehicleSpeed()
CruisedSpeedKm = TransformToKm(CruisedSpeed)
ESX.ShowNotification(_U('activated') .. ': ~b~ ' .. CruisedSpeedKm .. ' mp/h')
CreateThread(function ()
while CruisedSpeed > 0 and IsInVehicle() == PlayerPedId() do
Wait(0)
if not IsTurningOrHandBraking() and GetVehicleSpeed() < (CruisedSpeed - 1.5) then
CruisedSpeed = 0
ESX.ShowNotification(_U('deactivated'))
Wait(2000)
break
end
if not IsTurningOrHandBraking() and IsVehicleOnAllWheels(GetVehicle()) and GetVehicleSpeed() < CruisedSpeed then
SetVehicleForwardSpeed(GetVehicle(), CruisedSpeed)
end
if IsControlJustPressed(1, 246) then
CruisedSpeed = GetVehicleSpeed()
CruisedSpeedKm = TransformToKm(CruisedSpeed)
end
if IsControlJustPressed(2, 72) then
CruisedSpeed = 0
ESX.ShowNotification(_U('deactivated'))
Wait(2000)
break
end
end
end)
end
end
end
function TriggerAccel()
if CruisedSpeed > 0 and IsDriving() then
if GetVehicleSpeed() < (GetVehicleTopSpeed() + 4.25) and GetVehicleCurrentGear(GetVehicle()) > 0 then
Speed = GetVehicleSpeed()
CruisedSpeed = (GetVehicleSpeed() + 0.48)
TopSpeed = (GetVehicleTopSpeed() + 5.5)
CruisedSpeedKm = TransformToKm(CruisedSpeed)
--ESX.ShowNotification(_U('up') .. ': ~b~ ' .. CruisedSpeedKm .. ' mp/h') --This spams us with messages :(
CreateThread(function ()
while CruisedSpeed > 0 and IsInVehicle() == PlayerPedId() do
Wait(0)
if not IsTurningOrHandBraking() and GetVehicleSpeed() >= (GetVehicleTopSpeed() + 5.5) then
CruisedSpeed = CruisedSpeed
ESX.ShowNotification(_U('deactivated'))
Wait(2000)
break
end
if not IsTurningOrHandBraking() and IsVehicleOnAllWheels(GetVehicle()) then
if not Speed < TopSpeed then
SetVehicleForwardSpeed(GetVehicle(), CruisedSpeed)
end
end
if IsControlJustPressed(1, 246) then
CruisedSpeed = GetVehicleSpeed()
CruisedSpeedKm = TransformToKm(CruisedSpeed)
end
if IsControlJustReleased(2, 72) then
CruisedSpeed = CruisedSpeed
ESX.ShowNotification(_U('deactivated'))
Wait(2000)
break
end
end
end)
end
end
end
function TriggerDecel()
if CruisedSpeed > 1 and IsDriving() then
if GetVehicleSpeed() <= (GetVehicleTopSpeed() + 5.5) and GetVehicleCurrentGear(GetVehicle()) > 0 then
CruisedSpeed = (GetVehicleSpeed() - 0.48)
TopSpeed = GetVehicleTopSpeed()
CruisedSpeedKm = TransformToKm(CruisedSpeed)
--ESX.ShowNotification(_U('down') .. ': ~b~ ' .. CruisedSpeedKm .. ' mp/h') -- :(
CreateThread(function ()
while CruisedSpeed > 1 and IsInVehicle() == PlayerPedId() do
Wait(0)
if not IsTurningOrHandBraking() and GetVehicleSpeed() <= (GetVehicleTopSpeed() + 5.5) then
CruisedSpeed = CruisedSpeed
--ESX.ShowNotification(_U('deactivated'))
Wait(2000)
break
end
if not IsTurningOrHandBraking() and IsVehicleOnAllWheels(GetVehicle()) then
SetVehicleForwardSpeed(GetVehicle(), CruisedSpeed)
end
if IsControlJustPressed(1, 246) then
CruisedSpeed = GetVehicleSpeed()
CruisedSpeedKm = TransformToKm(CruisedSpeed)
end
if IsControlJustReleased(2, 72) then
CruisedSpeed = CruisedSpeed
ESX.ShowNotification(_U('deactivated'))
Wait(2000)
break
end
end
end)
end
end
end
function IsTurningOrHandBraking ()
return IsControlJustReleased(2, 76) or IsControlPressed(2, 63) or IsControlPressed(2, 64)
end
function IsDriving ()
return IsPedInAnyVehicle(PlayerPedId(), false)
end
function IsDrivingBannedVeh ()
return IsPedInAnyBoat(PlayerPedId()) or IsPedInAnyHeli(PlayerPedId()) or IsPedOnAnyBike(PlayerPedId())
end
function GetVehicle ()
return GetVehiclePedIsIn(PlayerPedId(), false)
end
function IsInVehicle ()
return GetPedInVehicleSeat(GetVehicle(), -1)
end
function IsDriver ()
return GetPedInVehicleSeat(GetVehiclePedIsIn(PlayerPedId(), false), -1)
end
function GetVehicleSpeed ()
return GetEntitySpeed(GetVehicle())
end
function GetVehicleTopSpeed ()
return GetVehicleEstimatedMaxSpeed(GetVehicle())
end
function TransformToKm (speed)
return math.floor(speed * 2.236936)
end
Also I give this altered code to any and all who would make use of it. I know there’s probably way better ones out there, but I’m just trying to get back into modding games, so any clues to what I did wrong here would be greatly appreciated.