[Free][Standalone] Drivetrain Sciprt

Drivetrain Pic

This Script Allows You To Change From AWD To FWD to RWD to 4WD

All you need to do is /AWD , /RWD , /FWD , FourWD

This will change the drivetrain for every car. There is also a config file that lets you customize the command that you would like to use.

Script Download

Updates
Got Rid Of About 40 lines Thanks To @DrAceMisanthrope
Also Added A Whitelist For It!
Also Got Rid Of The Config File!

Code is accessible Yes
Subscription-based No
Lines (approximately) 42
Requirements Standalone
Support Yes
4 Likes

i would think this ressource needs an vehicle whitelist, for all of the rp servers to be realistic and only certain vehicles can switch, maybe one vehicle whitelist per command, but this sounds really nice

i can implement a vehicle whitelist

Firstly, I appreciate you releasing something and not charging money for it, but may I offer some feedback? This is 92 lines long, not including the config. It also has 2 natives in each function that do nothing. They get values for no reason;

GetVehicleHandlingFloat(veh,'CHandlingData', 'fDriveBiasFront')
GetVehicleNumberOfWheels(veh)

These 92+ lines can be reduced to 22.

local DriveBias = {
	["FWD"] = 1.0,
	["RWD"] = 0.0,
	["AWD"] = 0.02,
	["FourWD"] = 0.5,
}

function SetDriveChain(driveType)
	local ped = PlayerPedId()
	local veh = GetVehiclePedIsIn(ped, false)
	if veh ~= 0 and GetPedInVehicleSeat(veh, -1) == ped then
		SetVehicleHandlingField(veh, 'CHandlingData', 'fDriveBiasFront', DriveBias[driveType])
		SetVehicleWheelIsPowered(veh, 0, driveType ~= "RWD")
		SetVehicleWheelIsPowered(veh, 1, driveType ~= "RWD")
		SetVehicleWheelIsPowered(veh, 3, driveType ~= "FWD")
		SetVehicleWheelIsPowered(veh, 4, driveType ~= "FWD")
	end
end

for driveType,_ in pairs(DriveBias) do
	RegisterCommand(driveType, function() SetDriveChain(driveType) end)
end

Or 29 if you want to include the option for command names to be changed.

local DriveCommands = {
	["FWD"] = "FWD",
	["RWD"] = "RWD",
	["AWD"] = "AWD",
	["FourWD"] = "FourWD",
}

local DriveBias = {
	["FWD"] = 1.0,
	["RWD"] = 0.0,
	["AWD"] = 0.02,
	["FourWD"] = 0.5,
}

function SetDriveChain(driveType)
	local ped = PlayerPedId()
	local veh = GetVehiclePedIsIn(ped, false)
	if veh ~= 0 and GetPedInVehicleSeat(veh, -1) == ped then
		SetVehicleHandlingField(veh, 'CHandlingData', 'fDriveBiasFront', DriveBias[driveType])
		SetVehicleWheelIsPowered(veh, 0, driveType ~= "RWD")
		SetVehicleWheelIsPowered(veh, 1, driveType ~= "RWD")
		SetVehicleWheelIsPowered(veh, 3, driveType ~= "FWD")
		SetVehicleWheelIsPowered(veh, 4, driveType ~= "FWD")
	end
end

for driveType,cmdName in pairs(DriveCommands) do
	RegisterCommand(cmdName, function() SetDriveChain(driveType) end)
end

Feel free to push this update to GitHub.

1 Like

Thanks a lot im newer to coding so a lot of times I can use stuff that has better alternatives. I changed the code that I had and replaced it with yours and also added a whitelist system. Again thank you very much!

1 Like

added

thank you really much

Just a few more tweaks to improve things (other than the incorrect indenting :stuck_out_tongue_winking_eye: ); you are using GetHashKey for the new vehicle whitelist which is inherently slower than Jenkins hashing. That is when you use backticks for a string and FiveM will treat it as the hash of the string. It’s converted at runtime as well, whereas the way you had it, it gets the hash for every vehicle, every time the command is called.

Example of Jenkins hashing;

local WLVeh = {
	`adder`,
}

Note the backticks instead of single/double quotes. This is like GetHashKey(“adder”) but faster.

Also, once the vehicle has been found, you should break from the loop or return the function, so you don’t loop, find the vehicle and set the drive chain, then keep looping for no reason until the end of the table.

And finally, you are getting the vehicle model and looping through the WLVeh table, comparing the vehicle model to the WLVeh table, then checking if they are even in a vehicle and are the driver. Do that check first or you could be fetching the model and then looping a table for a non-existent vehicle.

So what you probably want is this;

local WLVeh = {
	`adder`,
}

function SetDriveChain(driveType)
	local ped = PlayerPedId()
	local veh = GetVehiclePedIsIn(ped, false)
	if veh ~= 0 and GetPedInVehicleSeat(veh, -1) == ped then
		local model = GetEntityModel(veh)
		for i = 1, #WLVeh do
			if WLVeh[i] == model then
				SetVehicleHandlingField(veh, 'CHandlingData', 'fDriveBiasFront', DriveBias[driveType])
				SetVehicleWheelIsPowered(veh, 0, driveType ~= "RWD")
				SetVehicleWheelIsPowered(veh, 1, driveType ~= "RWD")
				SetVehicleWheelIsPowered(veh, 3, driveType ~= "FWD")
				SetVehicleWheelIsPowered(veh, 4, driveType ~= "FWD")
				return
			end
		end
	end
end

HOWEVER, I am a bit of a perfectionist at times and this can be improved AGAIN. Instead of looping a table at all, you could just use key values for WLVeh, like this;

local WLVeh = {
	[`adder`] = true,
}

function SetDriveChain(driveType)
	local ped = PlayerPedId()
	local veh = GetVehiclePedIsIn(ped, false)
	if veh ~= 0 and GetPedInVehicleSeat(veh, -1) == ped and WLVeh[GetEntityModel(veh)] then
		SetVehicleHandlingField(veh, 'CHandlingData', 'fDriveBiasFront', DriveBias[driveType])
		SetVehicleWheelIsPowered(veh, 0, driveType ~= "RWD")
		SetVehicleWheelIsPowered(veh, 1, driveType ~= "RWD")
		SetVehicleWheelIsPowered(veh, 3, driveType ~= "FWD")
		SetVehicleWheelIsPowered(veh, 4, driveType ~= "FWD")
	end
end

That should be the fastest possible solution. Again, feel free to push this to GitHub.

EDIT: Don’t forget the other stuff I didn’t include. The loop that registers commands, the commands table and the drive bias table. This code block is only for what was changed. The rest was good. Also, the README.md and the fxmanifest.lua both need updating as they mention the config.lua file that no longer exists.

Oh my gosh, I noticed I called it drivechain, not drivetrain… I’m an idiot.
Here… Please use this instead :stuck_out_tongue: hahaha
It’s the full code for the client file too.

cl_drivetrain.lua:

-- Add Vehicles Here
local VehicleWhitelist = {
	[`adder`] = true,
}

-- Command Names
local DriveCommands = {
	["FWD"] = "FWD",
	["RWD"] = "RWD",
	["AWD"] = "AWD",
	["FourWD"] = "FourWD",
}

-- Dont Touch
local DriveBias = {
	["FWD"] = 1.0,
	["RWD"] = 0.0,
	["AWD"] = 0.02,
	["FourWD"] = 0.5,
}

function SetDrivetrain(driveType)
	local ped = PlayerPedId()
	local veh = GetVehiclePedIsIn(ped, false)
	if veh ~= 0 and GetPedInVehicleSeat(veh, -1) == ped and VehicleWhitelist[GetEntityModel(veh)] then
		SetVehicleHandlingField(veh, 'CHandlingData', 'fDriveBiasFront', DriveBias[driveType])
		SetVehicleWheelIsPowered(veh, 0, driveType ~= "RWD")
		SetVehicleWheelIsPowered(veh, 1, driveType ~= "RWD")
		SetVehicleWheelIsPowered(veh, 3, driveType ~= "FWD")
		SetVehicleWheelIsPowered(veh, 4, driveType ~= "FWD")
	end
end

for driveType,cmdName in pairs(DriveCommands) do
	RegisterCommand(cmdName, function() SetDrivetrain(driveType) end)
end

Consider keybinds instead of chat commands?