Repairing cars

Hello, I’m new to scripting in GTA 5 and Lua, etc. etc.
I’ve found a script:


It’s a car damage system, pretty basic. But this script doesn’t include a repairing system. I wanted to create a simple command: /repair that allows a player to repair his car (only if he is inside this car). I think I made this but…

client.lua

function GetVehHealthPercent()
	local ped = GetPlayerPed(-1)
	local vehicle = GetVehiclePedIsUsing(ped)
	local vehiclehealth = GetEntityHealth(vehicle) - 100
	local maxhealth = GetEntityMaxHealth(vehicle) - 100
	local procentage = (vehiclehealth / maxhealth) * 100
	return procentage
end




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


Citizen.CreateThread(function()
	while true do
	Citizen.Wait(0)
		local ped = GetPlayerPed(-1)
		local vehicle = GetVehiclePedIsUsing(ped)
		local damage = GetVehHealthPercent(vehicle)
		if IsPedInAnyVehicle(ped, false) then
			SetPlayerVehicleDamageModifier(PlayerId(), 100) -- Seems to not work at the moment --
			if damage < 95 then
				SetVehicleUndriveable(vehicle, true)
				ShowNotification("~r~Silnik zostal uszkodzony.")
			end
		end
	end
end)

-- Repairing cars  
-- My own part here

RegisterNetEvent('RepairVehicle')
AddEventHandler('RepairVehicle', function()
	local playerPed = PlayerPedId()
	if IsPedInAnyVehicle(playerPed, false) then 
		local vehicle = GetVehiclePedIsUsing(playerPed, false)
		SetVehicleEngineHealth(vehicle, 100)
		SetVehicleEngineOn(vehicle, true, true)
		SetVehicleFixed(vehicle)
		ShowNotification("~g~Silnik zostal naprawiony.")
	else 
		ShowNotification("~r~Nie jestes w pojezdzie!")	
	end
end)

and now server.lua

AddEventHandler('chatMessage', function(source, n, msg)
	local msg = string.lower(msg)
	if msg == "/repair" then 
		TriggerClientEvent('RepairVehicle', source) 
	end
end)

It’s somehow working, but I have questions.

  1. Is it coded properly or what’s bad there? (The 1st part of car health etc. is not mine, but @hbk . 2nd part is mine.

  2. if IsPedInAnyVehicle(playerPed, false) then

Why do I have to use ‘false’ here? I have more similar questions so I would be happy if someone could tell me about this more on discord or pm! :smile:

I’m more of a C# guy but I can give you some pointers


Please use RegisterCommand instead. This way of parsing the chatMessage event is deprecated. See http://docs.fivem.net/scripting/#expanding-on-this for an example


Your RepairVehicle is coded nicely. Good use of the playerPed variable. Most beginners re-use PlayerPedId() and that’s definitely not the way to go. Good job.


As for your question about IsPedInAnyVehicle(playerPed, false):

If you look at the reference you can see an explanation of this boolean. https://runtime.fivem.net/doc/reference.html#_0x997ABD671D25CA0B

It’s pretty much determining when somebody is in a vehicle. When they open the car door, or when they are actually seated. true means when they “touch the vehicle door handle”, false when they are seated

Haha, thank you very much! This site https://runtime.fivem.net/doc/reference.html answered basically on most of my questions.
I have one more right now, it’s a bit late so I may think not properly :smile:

SetVehicleEngineHealth(vehicle, 100)

It’s set to 100 because of

if damage < 95 then

But now I found out that maximum vehicle HP is 1000. Is there any difference between setting it to 100 or 1000?
Btw. sorry for my english misspellings, it’s not my native language :wink:

Your English is perfectly fine.

If the maximum HP of a vehicle is 1000, it’s best to set it to fully repaired (1000). Otherwise the tiniest scratch could make it undriveable again. I’m not too familiar with vehicle health.

Your check for the damage just says that if it’s under 95 it will make it undriveable. You can change that value to whatever you want.

Allright, I’ve been practicing some scripting, but I have a question.
I have added

RegisterCommand('fix', function(source)
	TriggerEvent('RepairVehicle')
end)

As you wish, but why when notification of crashed engine (it takes some time until it hides btw.), notification after typing “/fix” isn’t popping up or it pops up really rarely? Everything is working (car is being fixed) except notifications, they seem lagged.
If anyone know the answer, I would be happy ;D

After some experiments, I’ve found out that when I changed Citizen.Wait(0) to Citizen.Wait(200) everything works perfectly, notifications aren’t lagged anymore.
My next question is, what does it do?
Citizen.CreateThread(function()
I can’t find anything on the google :confused:

how can i close /repair command ?

What do you mean by “close” ?

i mean deacitve (/repair) sorry my bad english

As permissions?

Either unload the resource that offers repair or add a block in the resource itself, to allow for certain users. IsPlayerAceAllowed

Basic Aces and Principles Guide

FiveM native Reference

Example:
server side…

RegisterServerEvent("permsRepair")
function callRepair(param)
  if IsPlayerAceAllowed(source, "administrator") then
    print("param: "..param)
    -- Trigger the client event to initiate repair ...
  else
    print("That user has no perms...") -- use TriggerClientEvent("chat:addMessage", source, ... here to send to player
  end
end
AddEventHandler("permsRepair", callRepair)

in your server.cfg you would add the ace perm for the person you want access to that command
add_ace Admin administrator allow