[LUA][Help] Commands to repair exploded vehicle: only showing repaired for player that entered it. Client vs Server?

FXServer

So in a nutshell, I wrote some commands to ‘rebuild’ an exploded+dead vehicle, which brings it back to ‘life’ (sort of) but then removes all doors, windows, tires, and makes it enterable but has no health (well, 1 HP since I use another script to prevent complete destruction from simply having low health.) Once ‘rebuilt’, the vehicle can then be fully ‘fixed’ and driven again. (Will soon attempt to implement jobs for mechanics, etc.)

Anyway, my point is this: the script for ‘rebuilding’ exploded vehicles only seems to work for the person who exploded that particular vehicle, and I’ve seen screenshares of friends testing this with me, and my ‘rebuilt+fixed’ vehicle(s) still show as exploded vehicles on their screen even though I’m driving around in them. Of course, my rebuilt+fixed vehicle looks perfectly fine on my screen. They can’t seem to rebuild or fix vehicles they themselves didn’t explode, and when they do, I still see those vehicles as exploded, despite them being driven around and appearing fine on said friends screen.

I’m thinking I need to do something with Networkxxx commands rather than what I’m using.

Client:

RegisterNetEvent('event:rebuild')
AddEventHandler('event:rebuild', function()
	local playerPed = GetPlayerPed(-1)
	local coordA = GetEntityCoords(playerPed, 1)
	local coordB = GetOffsetFromEntityInWorldCoords(playerPed, 0.0, 5.0, 0.0)
	local vehicle = GetVehiclePedIsIn(playerPed, false)
	local targetvehicle = getVehicleInDirection(coordA, coordB)
	local VehBodyHP = GetVehicleBodyHealth(vehicle)
	local tVehBodyHP = GetVehicleBodyHealth(targetvehicle)
	local VehDriveable = IsVehicleDriveable(vehicle, false)
	local tVehDriveable = IsVehicleDriveable(targetvehicle, false)
--	local curveh = GetVehiclePedIsUsing(playerPed) --this equates to sitting, driving, or *riding turrets (*I think), we only want driving

	if IsPedInAnyVehicle(playerPed, true) then
		if not VehDriveable or IsEntityDead(vehicle) then
			SetVehicleDeformationFixed(vehicle)
			SetVehicleFixed(vehicle)
			SetVehicleTyreBurst(vehicle, 0, true, 1000)
			SetVehicleTyreBurst(vehicle, 1, true, 1000)
			SetVehicleTyreBurst(vehicle, 2, true, 1000)
			SetVehicleTyreBurst(vehicle, 3, true, 1000)
			SetVehicleTyreBurst(vehicle, 4, true, 1000)
			SetVehicleTyreBurst(vehicle, 5, true, 1000)
			SetVehicleTyreBurst(vehicle, 6, true, 1000)
			SetVehicleTyreBurst(vehicle, 7, true, 1000)
			SetVehicleTyreBurst(vehicle, 8, true, 1000)
			SetVehicleTyreBurst(vehicle, 9, true, 1000)
			SetVehicleDoorBroken(vehicle, 0, true)
			SetVehicleDoorBroken(vehicle, 1, true)
			SetVehicleDoorBroken(vehicle, 2, true)
			SetVehicleDoorBroken(vehicle, 3, true)
			SetVehicleDoorBroken(vehicle, 4, true)
			SetVehicleDoorBroken(vehicle, 5, true)
			SetVehicleDoorBroken(vehicle, 6, true)
			SetVehicleDoorBroken(vehicle, 7, true)
			RemoveVehicleWindow(vehicle, 0)
			RemoveVehicleWindow(vehicle, 1)
			RemoveVehicleWindow(vehicle, 2)
			RemoveVehicleWindow(vehicle, 3)
			RemoveVehicleWindow(vehicle, 4)
			RemoveVehicleWindow(vehicle, 5)
			RemoveVehicleWindow(vehicle, 6)
			RemoveVehicleWindow(vehicle, 7)
			SetVehicleOilLevel(vehicle, 1.0)
			SetVehicleFuelLevel(vehicle, 1.0)
			SetVehicleEngineHealth(vehicle, 1.0)
			SetVehicleBodyHealth(vehicle, 1.0)
			SetVehiclePetrolTankHealth(vehicle, 1.0)
			SetVehicleEngineOn(vehicle, false, true, true)
			SetVehicleUndriveable(vehicle, true)
			ShowNotification("~g~This vehicle has been rebuilt and can now be fixed")
		else
			ShowNotification("~g~This vehicle doesn't need to be rebuilt")
		end
	else
		if targetvehicle ~= 0 and IsVehicleSeatFree(targetvehicle, -1) then
			if not tVehDriveable or IsEntityDead(targetvehicle) then
				SetVehicleDeformationFixed(targetvehicle)
				SetVehicleFixed(targetvehicle)
				SetVehicleTyreBurst(targetvehicle, 0, true, 1000)
				SetVehicleTyreBurst(targetvehicle, 1, true, 1000)
				SetVehicleTyreBurst(targetvehicle, 2, true, 1000)
				SetVehicleTyreBurst(targetvehicle, 3, true, 1000)
				SetVehicleTyreBurst(targetvehicle, 4, true, 1000)
				SetVehicleTyreBurst(targetvehicle, 5, true, 1000)
				SetVehicleTyreBurst(targetvehicle, 6, true, 1000)
				SetVehicleTyreBurst(targetvehicle, 7, true, 1000)
				SetVehicleTyreBurst(targetvehicle, 8, true, 1000)
				SetVehicleTyreBurst(targetvehicle, 9, true, 1000)
				SetVehicleDoorBroken(targetvehicle, 0, true)
				SetVehicleDoorBroken(targetvehicle, 1, true)
				SetVehicleDoorBroken(targetvehicle, 2, true)
				SetVehicleDoorBroken(targetvehicle, 3, true)
				SetVehicleDoorBroken(targetvehicle, 4, true)
				SetVehicleDoorBroken(targetvehicle, 5, true)
				SetVehicleDoorBroken(targetvehicle, 6, true)
				SetVehicleDoorBroken(targetvehicle, 7, true)
				RemoveVehicleWindow(targetvehicle, 0)
				RemoveVehicleWindow(targetvehicle, 1)
				RemoveVehicleWindow(targetvehicle, 2)
				RemoveVehicleWindow(targetvehicle, 3)
				RemoveVehicleWindow(targetvehicle, 4)
				RemoveVehicleWindow(targetvehicle, 5)
				RemoveVehicleWindow(targetvehicle, 6)
				RemoveVehicleWindow(targetvehicle, 7)
				SetVehicleOilLevel(targetvehicle, 1.0)
				SetVehicleFuelLevel(targetvehicle, 1.0)
				SetVehicleEngineHealth(targetvehicle, 1.0)
				SetVehicleBodyHealth(targetvehicle, 1.0)
				SetVehiclePetrolTankHealth(targetvehicle, 1.0)
				SetVehicleEngineOn(targetvehicle, false, true, true)
				SetVehicleUndriveable(targetvehicle, true)
				ShowNotification("~g~This vehicle has been rebuilt and can now be fixed")
			else
				ShowNotification("~g~This vehicle doesn't need to be rebuilt")
			end
		end
	end
end)

function getVehicleInDirection(coordFrom, coordTo)
	local rayHandle = CastRayPointToPoint(coordFrom.x, coordFrom.y, coordFrom.z, coordTo.x, coordTo.y, coordTo.z, 10, GetPlayerPed(-1), 0)
	local a, b, c, d, vehicle = GetRaycastResult(rayHandle)
	return vehicle
end

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

Server:

AddEventHandler('chatMessage', function(source, n, msg)
	local msg = string.lower(msg)
	local identifier = GetPlayerIdentifiers(source)[1]
	if msg == "/fix" then
		CancelEvent()
		if everyoneAllowed == true then
			TriggerClientEvent('event:fix', source)
		else
			if checkAllowed(identifier) then
				TriggerClientEvent('event:fix', source)
			else
				TriggerClientEvent('event:noPerms', source)
			end
		end
	elseif msg == "/rebuild" then 
		CancelEvent()
		if everyoneAllowed == true then
			TriggerClientEvent('event:rebuild', source)
		else
			if checkAllowed(identifier) then
				TriggerClientEvent('event:rebuild', source)
			else
				TriggerClientEvent('event:noPerms', source)
			end
		end
	end
end)

function checkAllowed(id)
	for k, v in pairs(allowed) do
		if id == v then
			return true
		end
	end
	
	return false
end

You should not use

Use:

local acePermissionsRequired = false

RegisterCommand('fix', function(source, args, rawCommand) -- Works on client and server
    -- code that runs when the command is executed goes here
end, acePermissionsRequired)

Do you think this will solve my problem(s)? Will all my Commands require all 3 (source, args, rawCommand)?

Not necessarily I am just saying this is a better method of making commands.

When I use your method, it keeps saying access denied, although I am a principal in server.cfg and I can use vsync just fine. Am I missing something here?

1 Like

First, you have to get control over vehicle, before doing something with it. Use these natives

bool NetworkHasControlOfEntity(entity)
void NetworkRequestControlOfEntity(entity)

Second, you can fully repair vehicles by native void SetVehicleFixed(vehicle)

Just assuming here, but I think ‘void’ has to do with C+, I’m using LUA as I don’t know C#/C+, so not sure how much help your comment provides, but thank you for your input.