Jump to content

Moving engine parts


Recommended Posts

Hello, I am working on script for moving engine parts. I managed to make parts move but at the moment it is only turnable by command and I need help with script doing it somewhat automatically when engine is running. The vehicles are server-sided. I tried using https://wiki.multitheftauto.com/wiki/GetNearestVehicle but only thing I managed to get is error from getElementPosition,Interior and Dimension everytime when I tried localPlayer, player or thePlayer for some reason although I don't know if it would work when used on client-side with server-sided vehicles and probably needs something more to it. Thank you in advance for any reply.

 

(PS: Plz don't kill me for my naming conventions 😄)

CLIENT

function epr()
	local theVeh = getPedOccupiedVehicle(source)
        
	if (theVeh) then
		setTimer(function()
			local rx, ry, rz = getVehicleComponentRotation(theVeh, "vetrak")
			setVehicleComponentRotation(theVeh, "vetrak", rx-25, ry, rz)
			local rx, ry, rz = getVehicleComponentRotation(theVeh, "kluka")
			setVehicleComponentRotation(theVeh, "kluka", rx-25, ry, rz)
			local rx, ry, rz = getVehicleComponentRotation(theVeh, "alternator")
			setVehicleComponentRotation(theVeh, "alternator", rx-25, ry, rz)
		end, 0, 0)
	end
end
addEvent("epr", true)
addEventHandler("epr", root, epr)

 

SERVER

function epr( thePlayer, commandName )
    triggerClientEvent (thePlayer,"epr",thePlayer)
end
addCommandHandler ("epr", epr)

 

  • Like 1
Link to comment

Well it works but only on the nearest vehicle (yeah I know that is logical) but does somebody know how to make it work on more cars nearby?

 

function getNearestVehicle(source,distance)
    local lastMinDis = distance-0.0001
    local nearestVeh = false
    local px,py,pz = getElementPosition(source)
    local pint = getElementInterior(source)
    local pdim = getElementDimension(source)

    for _,v in pairs(getElementsByType("vehicle")) do
        local vint,vdim = getElementInterior(v),getElementDimension(v)
        if vint == pint and vdim == pdim then
            local vx,vy,vz = getElementPosition(v)
            local dis = getDistanceBetweenPoints3D(px,py,pz,vx,vy,vz)
            if dis < distance then
                if dis < lastMinDis then 
                    lastMinDis = dis
                    nearestVeh = v
                end
            end
        end
    end
    return nearestVeh
end

function getElementSpeed(theElement, unit)
    -- Check arguments for errors
    assert(isElement(theElement), "Bad argument 1 @ getElementSpeed (element expected, got " .. type(theElement) .. ")")
    local elementType = getElementType(theElement)
    assert(elementType == "player" or elementType == "ped" or elementType == "object" or elementType == "vehicle" or elementType == "projectile", "Invalid element type @ getElementSpeed (player/ped/object/vehicle/projectile expected, got " .. elementType .. ")")
    assert((unit == nil or type(unit) == "string" or type(unit) == "number") and (unit == nil or (tonumber(unit) and (tonumber(unit) == 0 or tonumber(unit) == 1 or tonumber(unit) == 2)) or unit == "m/s" or unit == "km/h" or unit == "mph"), "Bad argument 2 @ getElementSpeed (invalid speed unit)")
    -- Default to m/s if no unit specified and 'ignore' argument type if the string contains a number
    unit = unit == nil and 0 or ((not tonumber(unit)) and unit or tonumber(unit))
    -- Setup our multiplier to convert the velocity to the specified unit
    local mult = (unit == 0 or unit == "m/s") and 50 or ((unit == 1 or unit == "km/h") and 180 or 111.84681456)
    -- Return the speed by calculating the length of the velocity vector, after converting the velocity to the specified unit
    return (Vector3(getElementVelocity(theElement)) * mult).length
end

addEventHandler( "onClientResourceStart", getRootElement( ),
    function (thePlayer)
        epr()
    end
);

function getVehicleRPM(vehicle)
    local vehicle = getNearestVehicle(localPlayer,2000)
    local vehicleRPM = 0
    if (vehicle) then
        if (getVehicleEngineState(vehicle) == true) then
            if getVehicleCurrentGear(vehicle) > 0 then
                vehicleRPM = math.floor(((getElementSpeed(vehicle, "km/h") / getVehicleCurrentGear(vehicle)) * 160) + 0.5)
                if (vehicleRPM < 650) then
                    vehicleRPM = math.random(650, 750)
                elseif (vehicleRPM >= 9000) then
                    vehicleRPM = math.random(9000, 9900)
                end
            else
                vehicleRPM = math.floor((getElementSpeed(vehicle, "km/h") * 160) + 0.5)
                if (vehicleRPM < 650) then
                    vehicleRPM = math.random(650, 750)
                elseif (vehicleRPM >= 9000) then
                    vehicleRPM = math.random(9000, 9900)
                end
            end
        else
            vehicleRPM = 0
        end

        return tonumber(vehicleRPM)
    end
end

function epr()
	setTimer(function()
		local theVeh = getNearestVehicle(localPlayer,2000)
		local enginerpm = getVehicleRPM(theVeh)
		local rotspeed = enginerpm/32
		local rx, ry, rz = getVehicleComponentRotation(theVeh, "vetrak")
		setVehicleComponentRotation(theVeh, "vetrak", rx-rotspeed, ry, rz)
		local rx, ry, rz = getVehicleComponentRotation(theVeh, "kluka")
		setVehicleComponentRotation(theVeh, "kluka", rx-rotspeed, ry, rz)
		local rx, ry, rz = getVehicleComponentRotation(theVeh, "alternator")
		setVehicleComponentRotation(theVeh, "alternator", rx-rotspeed, ry, rz)
	end, 0, 0)
end
addEvent("epr", true)
addEventHandler("epr", root, epr)

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...