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?