Skip to content

Commit

Permalink
Initial Release
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSmugGod authored Feb 14, 2024
1 parent 776d013 commit 3f944ee
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 0 deletions.
138 changes: 138 additions & 0 deletions SG-OB2/client.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@

-- Event handler for using the OBD2 scanner
RegisterNetEvent('obd2:ReadData')
AddEventHandler('obd2:ReadData', function(vehicle, vehicleNetId)
local playerPed = PlayerPedId()
local vehiclePed = GetVehiclePedIsIn(playerPed, false)
if vehiclePed then
local state = Entity(vehiclePed).state
local obd2Data = state.FaultData
if obd2Data then
-- print("OBD2 Data Found:", obd2Data)
-- Handle displaying the OBD2 data to the player

local faultAlert = lib.alertDialog({
header = 'Falut Code/s Detected!',
content = obd2Data,
centered = true,
cancel = true
})
print(faultAlert)
else
-- print("No OBD2 Data Found")
-- Handle notifying the player that no data is available
lib.notify({
title = 'OBD2 Scanner',
description = 'No Fault Codes Detected!',
type = 'success'
})
end
else
print("You must be in a vehicle to set OBD2 data.")
end
end)

RegisterNetEvent('obd2:UseScanner')
AddEventHandler('obd2:UseScanner', function(vehicle, vehicleNetId)
local playerPed = PlayerPedId()
local vehiclePed = GetVehiclePedIsIn(playerPed, false)
if vehiclePed then
local state = Entity(vehiclePed).state
local obd2Data = state.FaultData

lib.showContext('obd2_menu')

-- if obd2Data then
-- print("OBD2 Data Found:", obd2Data)
-- Handle displaying the OBD2 data to the player
-- else
-- print("No OBD2 Data Found")
-- Handle notifying the player that no data is available
-- end
else
print("You must be in a vehicle to set OBD2 data.")
end
end)

RegisterNetEvent("obd2:setData")
AddEventHandler("obd2:setData", function(vehicleNetId, obd2Data)
local vehicle = NetworkGetEntityFromNetworkId(vehicleNetId)
if DoesEntityExist(vehicle) then
Entity(vehicle).state.FaultData = obd2Data
print("Updated OBD2 Data to:", Entity(vehicle).state.FaultData)
else
print("Invalid vehicle network ID received:", vehicleNetId)
end
end)

-- Event handler to display engine status to the player
RegisterNetEvent('obd2:DisplayEngineStatus')
AddEventHandler('obd2:DisplayEngineStatus', function(engineDamaged)
if engineDamaged then
print("Vehicle engine is damaged ")
-- Handle notifying the player that the engine is damaged
else
-- print("Vehicle engine is not damaged")
-- Handle notifying the player that the engine is not damaged
end
end)

-- Event handler for when a vehicle/entity is damaged
AddEventHandler('entityDamaged', function(victim, attacker, weaponHash, damage)
if IsEntityAVehicle(victim) then
local vehicle = victim
local engineHealth = GetVehicleEngineHealth(vehicle)

if engineHealth > 675 and engineHealth < 800 then
-- Engine health is between 700 and 800
-- Set Data to P0128 - Coolant Temperature Below Thermostat Regulating Temperature
print(engineHealth)
TriggerServerEvent('obd2:UpdateTheDamage', vehicle, 'P0128 - Coolant Temperature Below Thermostat Regulating Temperature')
end

if engineHealth > 550 and engineHealth < 675 then
-- Engine health is between 400 and 700
-- Set Data to P0300 - Random or Multiple Cylinder Misfire Detected
TriggerServerEvent('obd2:UpdateTheDamage', vehicle, 'P0128 - Coolant Temperature Below Thermostat Regulating Temperature P0300 - Random or Multiple Cylinder Misfire Detected')
end

if engineHealth > 460 and engineHealth < 550 then
-- Engine health is between 400 and 700
-- Set Data to P0300 - Random or Multiple Cylinder Misfire Detected
TriggerServerEvent('obd2:UpdateTheDamage', vehicle, 'P0128 - Coolant Temperature Below Thermostat Regulating Temperature P0300 - Random or Multiple Cylinder Misfire Detected P0340 — Camshaft Position Sensor Circuit Malfunction')
end

--print("Vehicle Engine Health:", engineHealth)
end
end)


lib.registerContext({
id = 'obd2_menu',
title = 'OBD2 Scanner',
options = {
{
title = 'OBD2 Scanner Connected!',
},
{
title = 'Read Codes',
description = 'Scan and Read Fault Codes',
icon = 'magnifying-glass',
event = 'obd2:ReadData',
arrow = true
},
{
title = 'Clear Codes',
description = 'Scan and Read Fault Codes',
icon = 'eraser',
event = 'obd2:DeleteData',
arrow = true
}
}
})


RegisterNetEvent('obd2:DeleteData')
AddEventHandler('obd2:DeleteData', function()
print('coming soon')
end)
18 changes: 18 additions & 0 deletions SG-OB2/fxmanifest.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
fx_version 'cerulean'
game 'gta5'

author 'Smug God'
description 'OBD2 Scanner Script for FiveM'
version '1.0.0'
lua54 'yes'

-- Define the script dependency
dependencies {
'es_extended'
}

shared_script '@ox_lib/init.lua'

-- Specify the entry point for your script
client_script 'client.lua'
server_script 'server.lua'
98 changes: 98 additions & 0 deletions SG-OB2/server.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
ESX = exports["es_extended"]:getSharedObject()

-- Register the scanner as a usable item
ESX.RegisterUsableItem('obd2', function(source)
--local xPlayer = ESX.GetPlayerFromId(source)
local playerped = GetPlayerPed(source)
local vehicle = GetVehiclePedIsIn(playerped, false)
if vehicle then
local vehicleNetId = NetworkGetNetworkIdFromEntity(vehicle)
TriggerClientEvent('obd2:UseScanner', source, vehicle, vehicleNetId) -- Pass the vehicle to the clients
end
end)


RegisterCommand("set_obd2_data", function(source, args, rawCommand)
local playerped = GetPlayerPed(source)
local vehicle = GetVehiclePedIsIn(playerped, false)
if vehicle then
local obd2Data = args[1] -- Assuming the data is passed as the first argument to the command
local vehicleNetId = NetworkGetNetworkIdFromEntity(vehicle)
if vehicleNetId ~= 0 then
TriggerClientEvent("obd2:setData", source, vehicleNetId, obd2Data)
print("OBD2 data set successfully:", obd2Data)
else
print("Failed to get network ID for the vehicle.")
end
else
print("You must be in a vehicle to set OBD2 data.")
end
end, false)



-- Function to check if a vehicle engine is damaged or not working
function IsVehicleEngineDamaged(vehicle)
local engineHealth = GetVehicleEngineHealth(vehicle)
-- print (engineHealth)
-- Define a threshold value for engine health
local damageThreshold = 800
local engineLockedThreshold = 100

return engineHealth < damageThreshold or engineHealth <= engineLockedThreshold
end

-- command to check vehicle engine damage
RegisterCommand("check_vehicle_damage", function(source, args, rawCommand)
local playerPed = GetPlayerPed(source)
local vehicle = GetVehiclePedIsIn(playerPed, false)
if vehicle then
if IsVehicleEngineDamaged(vehicle) then
TriggerClientEvent('chatMessage', source, "^1Vehicle engine is damaged")
else
TriggerClientEvent('chatMessage', source, "^2Vehicle engine is not damaged")
end
else
TriggerClientEvent('chatMessage', source, "^1You are not in a vehicle")
end
end, false)


-- Function to update the engine status in the state bag
function UpdateEngineStatus(vehicle)
local engineDamaged = IsVehicleEngineDamaged(vehicle)
local state = Entity(vehicle).state
state.EngineDamaged = engineDamaged
end

-- Event handler to handle player using the OBD2 scanner
RegisterCommand("check_engine_status", function(source, args, rawCommand)
local playerPed = GetPlayerPed(source)
local vehicle = GetVehiclePedIsIn(playerPed, false)
if vehicle then
UpdateEngineStatus(vehicle)
local state = Entity(vehicle).state
local engineDamaged = state.EngineDamaged
TriggerClientEvent('obd2:DisplayEngineStatus', source, engineDamaged, vehicle)
else
-- TriggerClientEvent('chatMessage', source, "^1You are not in a vehicle")
end
end, false)

RegisterNetEvent('obd2:UpdateTheDamage')
AddEventHandler('obd2:UpdateTheDamage', function(veh, faultdata)
local playerped = GetPlayerPed(source)
local vehicle = GetVehiclePedIsIn(playerped, false)
if vehicle then
local obd2Data = faultdata -- Assuming the data is passed as the first argument to the command
local vehicleNetId = NetworkGetNetworkIdFromEntity(vehicle)
if vehicleNetId ~= 0 then
TriggerClientEvent("obd2:setData", source, vehicleNetId, obd2Data)
print("OBD2 data set successfully:", obd2Data)
else
print("Failed to get network ID for the vehicle.")
end
else
print("You must be in a vehicle to set OBD2 data.")
end
end)

0 comments on commit 3f944ee

Please sign in to comment.