Source Code
CarScript
local Car = script.Parent
local Seat = Car.VehicleSeat
local AutoPilot = Car.AutoPilot
local AutoPilotButton = Car.AutoPilotButton.ClickDetector
local FrontLeft = Car.FrontLeft
local FrontRight = Car.FrontRight
local BackLeft = Car.BackLeft
local BackRight = Car.BackRight
local SteerAngle = 30
local Speed = 60
local Player = nil
Seat:GetPropertyChangedSignal("Steer"):Connect(function()
if AutoPilot.Value == false then
FrontLeft.PartB.SteeringConstraint.TargetAngle = SteerAngle * Seat.Steer
FrontRight.PartB.SteeringConstraint.TargetAngle = SteerAngle * Seat.Steer
end
end)
Seat:GetPropertyChangedSignal("Throttle"):Connect(function()
if AutoPilot.Value == false then
FrontLeft.Wheel.WheelConstraint.AngularVelocity = Speed * Seat.Throttle
FrontRight.Wheel.WheelConstraint.AngularVelocity = Speed * -Seat.Throttle
BackLeft.Wheel.WheelConstraint.AngularVelocity = Speed * Seat.Throttle
BackRight.Wheel.WheelConstraint.AngularVelocity = Speed * -Seat.Throttle
end
end)
Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
local Humanoid = Seat.Occupant
if Humanoid then
Player = game.Players:GetPlayerFromCharacter(Humanoid.Parent)
else
Player = nil
end
end)
AutoPilot:GetPropertyChangedSignal("Value"):Connect(function()
if AutoPilot.Value == true then
FrontLeft.PartB.SteeringConstraint.TargetAngle = 0
FrontRight.PartB.SteeringConstraint.TargetAngle = 0
FrontLeft.Wheel.WheelConstraint.AngularVelocity = 0
FrontRight.Wheel.WheelConstraint.AngularVelocity = 0
BackLeft.Wheel.WheelConstraint.AngularVelocity = 0
BackRight.Wheel.WheelConstraint.AngularVelocity = 0
end
end)
AutoPilotButton.MouseClick:Connect(function()
AutoPilot.Value = true
if Player ~= nil then
local PlayerGui = Player.PlayerGui
local ScreenGui = PlayerGui.ScreenGui
ScreenGui.Enabled = true
wait(2)
ScreenGui.Enabled = false
end
end)
AIScript
local PathfindingService = game:GetService("PathfindingService")
local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(0.1, Enum.EasingStyle.Linear)
local Car = script.Parent
local AutoPilot = Car.AutoPilot
local Target = game.Workspace.Target
local Seat = script.Parent.VehicleSeat
local UIText = Car.Root.BillboardGui.TextLabel
local AlignPosition = Car.Root.AlignPosition
local AlignOrientation = Car.Root.AlignOrientation
AutoPilot:GetPropertyChangedSignal("Value"):Connect(function()
if AutoPilot.Value == true then
UIText.Text = "AUTO PILOT"
local Path = PathfindingService:CreatePath({
Costs = {
DangerZone = math.huge
}
})
Path:ComputeAsync(script.Parent.PrimaryPart.Position, Target.Position)
local Waypoints = Path:GetWaypoints()
for Index = 1, #Waypoints do
local CarCFrame = Car:GetPrimaryPartCFrame()
local CFrameValue = Instance.new("CFrameValue")
CFrameValue.Value = CarCFrame
local CurrentWayPoint = PathWaypoint.new(Vector3.new(Waypoints[Index].Position.X, CarCFrame.Position.Y, Waypoints[Index].Position.Z))
CFrameValue:GetPropertyChangedSignal("Value"):connect(function()
AlignPosition.Enabled = true
AlignOrientation.Enabled = true
AlignPosition.Position = CFrameValue.Value.Position
AlignOrientation.CFrame = CFrameValue.Value
end)
if Waypoints[Index + 1] ~= nil then
local NextWayPoint = PathWaypoint.new(Vector3.new(Waypoints[Index + 1].Position.X, CarCFrame.Position.Y, Waypoints[Index + 1].Position.Z))
local Tween = TweenService:Create(CFrameValue, Info, {Value = CFrame.new(CurrentWayPoint.Position, NextWayPoint.Position)})
Tween:Play()
Tween.Completed:Wait()
else
local Tween = TweenService:Create(CFrameValue, Info, {Value = CFrame.new(CurrentWayPoint.Position)})
Tween:Play()
Tween.Completed:Wait()
end
CFrameValue:Destroy()
end
AutoPilot.Value = false
AlignPosition.Enabled = false
AlignOrientation.Enabled = false
else
UIText.Text = "MANUAL"
end
end)
Starter Project
Starter Project - Auto Pilot Car Simulator.rbxl (215.4 KB)
