I am trying to make my car turn using an AngularVelocity but there is a problem that the car would just stay rotated like that.
Code:
local RunService = game:GetService("RunService")
local model = script.Parent
local Parts = model:WaitForChild("Parts")
local VehicleSeat = Parts:WaitForChild("VehicleSeat")
local WheelParts = model:WaitForChild("WheelParts")
local MoveVelocity = model:WaitForChild("MoveVelocity")
local TurnVelocity = model:WaitForChild("TurnVelocity")
local Occupant = model:WaitForChild("Occupant")
local speed = 50
local turnSpeed = 5
MoveVelocity.LineVelocity = speed
local driveConnection
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {model}
local function Drive()
driveConnection = RunService.Heartbeat:Connect(function()
for i,v in WheelParts:GetChildren() do
if v:IsA("BasePart") then
local rayOrigin = v.Position
local rayDirection = v.CFrame.UpVector * -1
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if raycastResult then
MoveVelocity.Enabled = true
TurnVelocity.AngularVelocity = Vector3.new(0, -(VehicleSeat.Steer * turnSpeed), 0)
break
else
TurnVelocity.AngularVelocity = Vector3.new(0, 0, 0)
MoveVelocity.Enabled = false
end
end
end
end)
end
local function StopDrive()
if driveConnection then
driveConnection:Disconnect()
driveConnection = nil
end
end
Occupant:GetPropertyChangedSignal("Value"):Connect(function()
if Occupant.Value then
Drive()
else
StopDrive()
end
end)