How can I fix my car?

I am trying to make all my cars go a constant, steady speed. I decided to start off with the smallest car. However, lots of things are going wrong. The car seems like it’s speeding up, and also it slides a lot when it turns. Here is the script responsible for making all my cars move:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local ContextActionService = game:GetService("ContextActionService")
local runService = game:GetService("RunService")
local inCar = false

local moveSpeed = 0
local turnSpeed = 0

local vehicleSeat
local vehicle
local configurations

local function onSit(Sitting, seat)
	if Sitting == true then
		if seat:IsA("VehicleSeat") then
			inCar = true
			vehicleSeat = seat
			vehicle = seat.Parent
			configurations = vehicle.Configuration
			
			ContextActionService:BindAction("flip", flipCar, false, "v")
			ContextActionService:BindAction("forward", moveForward, false, "w")
			ContextActionService:BindAction("backward", moveBack, false, "s")
			ContextActionService:BindAction("right", turnRight, false, "d")
			ContextActionService:BindAction("left", turnLeft, false, "a")
		end
	else
		inCar = false
		ContextActionService:UnbindAction("flip")
		ContextActionService:UnbindAction("forward")
		ContextActionService:UnbindAction("backward")
		ContextActionService:UnbindAction("right")
		ContextActionService:UnbindAction("left")
	end
end

function flipCar(name, inputState)
	if name == "flip" then
		if inCar then
			if inputState == Enum.UserInputState.Begin then
				local seat = vehicleSeat
				local bodyGyro = Instance.new("BodyGyro", seat)
				
				repeat wait() 
					if bodyGyro then
						seat:FindFirstChild("BodyGyro").MaxTorque = Vector3.new(1000, 0, 1000)
					end 
				until seat.Orientation.Z < 0.3 and seat.Orientation.Z > -0.1
				
				bodyGyro:Destroy()
			end
		end
	end
end

function moveForward(name, inputState)
	if name == "forward" then
		if inCar then
			if inputState == Enum.UserInputState.Begin then
				moveSpeed = configurations.MoveFactor.Value
			end
			
			if inputState == Enum.UserInputState.End then
				moveSpeed = Vector3.new(0, 0, 0)
			end
		end
	end
end

function moveBack(name, inputState)
	if name == "backward" then
		if inCar then
			if inputState == Enum.UserInputState.Begin then
				moveSpeed = -configurations.MoveFactor.Value
			end
			
			if inputState == Enum.UserInputState.End then
				moveSpeed = Vector3.new(0, 0, 0)
			end
		end
	end
end

function turnRight(name, inputState)
	if name == "right" then
		if inCar then
			if inputState == Enum.UserInputState.Begin then
				turnSpeed = -configurations.TurnFactor.Value
			end
			
			if inputState == Enum.UserInputState.End then
				turnSpeed = 0
			end
		end
	end
end

function turnLeft(name, inputState)
	if name == "left" then
		if inCar then
			if inputState == Enum.UserInputState.Begin then
				turnSpeed = configurations.TurnFactor.Value
			end
			
			if inputState == Enum.UserInputState.End then
				turnSpeed = 0
			end
		end
	end
end

runService:BindToRenderStep("Update", Enum.RenderPriority.Input.Value, function()
	if inCar then
		vehicleSeat.BodyVelocity.Velocity = vehicle.PrimaryPart.CFrame.lookVector * moveSpeed
		vehicleSeat.BodyAngularVelocity.AngularVelocity = Vector3.new(0, turnSpeed, 0)
	end
end)

humanoid.Seated:Connect(onSit)

I used BodyVelocity to move the car, which should make it move a constant speed while using BodyAngularVelocity to turn the car. However, the car’s movement gets a little weird. How can I fix this annoying problem?

So, you’re trying to make a car that rides smoothly and doesn’t slide a lot right?

Yes, I am trying to make a car avoid sliding. I wanna make the cars move a constant speed like they don’t gain speed or slow down gradually. But my car slides a lot and there are times where it doesn’t move at all.

Try this.

Get two cylinders and make them look like wheels.

after that open up Properties and Explorer and click on both wheels. Once you’ve done that, open up Properties and scroll down till you see something called Surface, Open it up and click on the RightSurface. Change the RightSurface to Hinge.

Once you have done that duplicate both wheels and spread them apart so it looks like this.

Now, you have the wheels but you can see the hinges on the other part of the wheels are facing the right side so what you need to do is click on both of the other wheels, Open up Properties and change the RightSurface to smooth and the LeftSurface to Hinge.

Now, you have the wheels done but you can drive the car so in order to drive it, follow what I do in the video below.

After you have done that insert a VehicleSeat into Workspace and place it on the car so it looks like this.

Now, you can drive a car. You can edit how fast it goes and the drift speed if you click on the VehicleSeat and open it up in Properties. If you have any questions just let me know.

1 Like

This is helpful but I am intending to make my own system. I don’t really wanna use Roblox’s system because it doesn’t make the car drive the way I want it to.

1 Like