How to script a car?

Hi Developers! Today, I needed some help to code a car! I’ve been trying to code it since a while now, I have also read the Basic Car Tutorial but it didn’t help me out much. Could you all please help me? Thanks.

There a bunch more car tutorials.

Try following this one:

3 Likes

Here’s a car tutorial:
Building a Basic Car (roblox.com)
(except you already read it, this is just for future reference)

To script one, well you don’t need to! But… I’ll tell you what I did.

I made a car chassis, and it involved using stuff like SpringConstraints and CylindricalConstraints (for steering).

The chassis was made around 2020, so expect some deprecated uses.


First, in the CarScript, I assigned some simple variables:

local useAllWheeldrive = false --Made possible using HingeConstraint

--local useAllWheelBraking = false --Made possible using HingeConstraint, but is not out yet.

local useTractionControl = false -- this feature is glitchy, better not to use as it is still in development.

local torqueMultiplier = 100 --Something like 100 to 1000

local steerTuning = 10--The multiplier to the magnitude rot velocity of which will make the steering keep the car from flipping over.



local seat = script.Parent.DriveSeat
local flw = script.Parent.FrontLeft
local frw = script.Parent.FrontRight
local blw = script.Parent.BackLeft
local brw = script.Parent.BackRight
local fls = script.Parent.SuspentionLowerFrontLeft.CylindricalConstraint
local frs = script.Parent.SuspentionLowerFrontRight.CylindricalConstraint
local bls = script.Parent.SuspentionLowerBackLeft--may not need this or
local brs = script.Parent.SuspentionLowerBackRight--this
local movement = Vector2.new(0,0)

Most of these are just references to some parts and some motors that the car needs to control to move and steer. However, movement is a vector with the seat’s throttle and steer (in variable format), as shown below:

seat.Changed:Connect(function(attribute)
	if attribute=="Throttle" then
		movement=Vector2.new(movement.X, seat.Throttle*seat.MaxSpeed)
	end
	if attribute=="Steer" then
		movement=Vector2.new(seat.Steer, movement.Y)
	end
	--print(movement)--Debug mode
end)

The movement variable isn’t necessary at all, but I feel it cleans up the code quite a bit.

We’re not done yet, there’s a few more things to go through.
First of all, we need to actually move the wheels.

game:GetService("RunService").Stepped:Connect(function()

	movement=Vector2.new(seat.Steer*(50-(((seat.RotVelocity.Magnitude)+seat.Velocity.Magnitude/100)*steerTuning)), movement.Y)--Steer emulation

	brw.HingeConstraint.AngularVelocity=-movement.Y
	blw.HingeConstraint.AngularVelocity=movement.Y

	fls.TargetAngle=movement.X
	frs.TargetAngle=movement.X
	frs.AngularSpeed=seat.TurnSpeed
	fls.AngularSpeed=seat.TurnSpeed

	if useTractionControl==true then
		game:GetService("RunService").Stepped:Connect(function()
			if seat.Velocity.Magnitude>=seat.MaxSpeed then
				seat.Velocity=seat.Velocity/1.01
				seat.RotVelocity=seat.RotVelocity/1.01
			end
		end)
	end

	if useAllWheeldrive==true then
		flw.HingeConstraint.MotorMaxTorque=seat.Torque*torqueMultiplier
		frw.HingeConstraint.MotorMaxTorque=seat.Torque*torqueMultiplier
		flw.HingeConstraint.MotorMaxAcceleration=script.Parent.Acceleration.Value
		frw.HingeConstraint.MotorMaxAcceleration=script.Parent.Acceleration.Value
		flw.HingeConstraint.AngularVelocity=movement.Y
		frw.HingeConstraint.AngularVelocity=-movement.Y
	end
	blw.HingeConstraint.MotorMaxTorque=seat.Torque*torqueMultiplier
	brw.HingeConstraint.MotorMaxTorque=seat.Torque*torqueMultiplier
	blw.HingeConstraint.MotorMaxAcceleration=script.Parent.Acceleration.Value
	brw.HingeConstraint.MotorMaxAcceleration=script.Parent.Acceleration.Value
end)

This might look like a mouthful, but the code is quite messy. Also don’t use traction control it doesn’t work really well.

As you can see, the steering is handled based on the movement vector mentioned earlier. This goes the same with the throttle.

However, the speed here isn’t multiplied by the wheel size, causing the car to go faster than the real speed. The speed must be multiplied by WheelSize / 2 I think.


The script is mostly done, but there’s more things to initialize:


--Setup
local d = script.Parent:GetDescendants()
for i=1,#d do
	if d[i].Name=="SpringConstraint" then
		local spring = d[i]
		spring.Stiffness=script.Parent.SpringStiffness.Value
		spring.FreeLength=script.Parent.SpringLength.Value
	end
end

if seat.MaxSpeed>=278 and seat.Torque>=19 then
	print("The seat MaxSpeed is greater then 278, and the torque is over 19.\nThis will result in the vehicle's wheels to experience difficulty, clipping to max possible.\nLocation: "..script:GetFullName())
	seat.MaxSpeed=250
	seat.Torque=45
end--Max possible.

seat.MaxSpeed=seat.MaxSpeed+3--Makes it more accurate.

None of that is necessary.

4 Likes

The Building a Basic Car tutorial scripting section tells you everything you need to know.

What isn’t working?
Is the car not moving?
Is it not turning?
Is the suspension falling apart?
Is there an error in the Output window?

You need to make sure you have all the Parts named, organized and referenced the same way the Roblox script does.
You have to make sure you don’t have any Anchored Parts in your car Model.
Click on the Show Constraints and Show on Top tools so you can see the Attachments and Constraints to see if they are misaligned.

This is very important. You have to manually set the Properties of the wheel HingeConstraints or CylindricalConstraints the same way that the tutorial says to.

2 Likes

Thank you all for the support, I appreciate it a lot! Thanks again!

I did try to follow this tutorial, although I wanted suspension, so yeah. Thanks though!

Thank you so much for taking so much effort for writing down all this, I appreciate it!

If someone solved the problem please mark their post as the Solution.
This helps others looking for solved posts about cars with that answer.