local y=script.Parent local Z=game:GetService("\082\117\110\083\101\114\118\105\099\101")local s=y.System.Wheels local A=y.System.VehicleSeat local M=A.ATSVariables local h={"\087\104\101\101\108\115"}function WeldPair(y,Z)local s=Instance.new("\087\101\108\100",y)s.Part0=y s.Part1=Z s.C1=Z.CFrame:toObjectSpace(y.CFrame)end function WeldAll(y,Z)for Z,s in pairs(h)do if y.Name==s then return end end if y:IsA("\066\097\115\101\080\097\114\116")then WeldPair(Z,y)end for y,s in pairs(y:GetChildren())do WeldAll(s,Z)end end function UnanchorAll(y)if y:IsA("\066\097\115\101\080\097\114\116")then y.Anchored=false end for y,Z in pairs(y:GetChildren())do UnanchorAll(Z)end end function Joint(y,Z,s,A,M)local h=Instance.new(M,y)h.Part0=y h.Part1=Z h.C0=s h.C1=A return h end function CreateWheelPart()local y=Instance.new("\080\097\114\116")y.FormFactor=Enum.FormFactor.Custom y.Transparency=1 y.Anchored=true y.CanCollide=false y.TopSurface=Enum.SurfaceType.Unjoinable y.BottomSurface=Enum.SurfaceType.Unjoinable y.LeftSurface=Enum.SurfaceType.Unjoinable y.RightSurface=Enum.SurfaceType.Unjoinable y.FrontSurface=Enum.SurfaceType.Unjoinable y.BackSurface=Enum.SurfaceType.Unjoinable return y end for y,Z in pairs(s:GetChildren())do if Z:IsA("\066\097\115\101\080\097\114\116")then local y=Z.Size.x local A=Instance.new("\077\111\100\101\108",s)A.Name=Z.Name Z.Parent=A Z.Name="\087\104\101\101\108"for y,s in pairs(Z:GetChildren())do if s:IsA("\066\097\115\101\080\097\114\116")then s.Parent=A WeldPair(Z,s)s.CanCollide=false end end if Z:FindFirstChild("\083\116\101\101\114")then local s=CreateWheelPart()s.Name="\083\116\101\101\114"s.Size=Vector3.new(y,1,.6)s.CFrame=Z.CFrame s.Parent=A end local h=CreateWheelPart()h.Name="\083\117\115\112\101\110\115\105\111\110"h.Size=Vector3.new(.8,1,y)h.CFrame=Z.CFrame*CFrame.new(-1,0,0)h.Parent=A local t=Instance.new("\066\111\100\121\071\121\114\111")t.Name="\066\071"if Z:FindFirstChild("\083\116\101\101\114")then t.D=M.SuspensionDampeningFront.Value else t.D=M.SuspensionDampeningRear.Value end t.P=M.SuspensionPower.Value t.maxTorque=Vector3.new()t.Parent=h local Q=CreateWheelPart()Q.Name="\066\111\100\121\072\105\110\103\101"Q.Size=Vector3.new(.8,1,y)Q.CFrame=Z.CFrame*CFrame.new(1,0,0)Q.Parent=A if Z:FindFirstChild("\080\111\119\101\114\101\100")then local y=Instance.new("\066\111\100\121\065\110\103\117\108\097\114\086\101\108\111\099\105\116\121")y.Name="\066\065\086"y.angularvelocity=Vector3.new()y.maxTorque=Vector3.new(20000,0,20000)y.P=800 y.Parent=Z end end end for y,Z in pairs(s:GetChildren())do if Z:FindFirstChild("\087\104\101\101\108")and Z:FindFirstChild("\083\117\115\112\101\110\115\105\111\110")then local y=Z:FindFirstChild("\083\116\101\101\114")~=nil if Z:FindFirstChild("\077\101\115\104")then WeldAll(Z.Mesh,Z.Wheel)end if y then Joint(Z.Wheel,Z.Steer,CFrame.Angles(math.pi/2,0,0),Z.Steer.CFrame:toObjectSpace(Z.Wheel.CFrame)*CFrame.Angles(math.pi/2,0,0),"\082\111\116\097\116\101")local y=Joint(Z.Steer,Z.Suspension,CFrame.Angles(math.pi,0,0),Z.Suspension.CFrame:toObjectSpace(Z.Steer.CFrame)*CFrame.Angles(math.pi,0,0),"\077\111\116\111\114")y.MaxVelocity=.03 else Joint(Z.Wheel,Z.Suspension,CFrame.Angles(math.pi/2,0,0),Z.Suspension.CFrame:toObjectSpace(Z.Wheel.CFrame)*CFrame.Angles(math.pi/2,0,0),"\082\111\116\097\116\101")end Joint(Z.Suspension,Z.BodyHinge,CFrame.new(Vector3.new(2,0,0))*CFrame.Angles(math.pi/2,0,0),(Z.BodyHinge.CFrame:toObjectSpace(Z.Suspension.CFrame)*CFrame.new(Vector3.new(2,0,0)))*CFrame.Angles(math.pi/2,0,0),"\082\111\116\097\116\101")Joint(Z.BodyHinge,A,CFrame.new(),A.CFrame:toObjectSpace(Z.BodyHinge.CFrame),"\087\101\108\100")else print("\073\110\118\097\108\105\100 \119\104\101\101\108\058 \077\105\115\115\105\110\103 \087\104\101\101\108 \111\114 \083\117\115\112\101\110\115\105\111\110 \112\097\114\116\033")end end WeldAll(y,A)wait(.5)UnanchorAll(y)function UpdateWheels()if not s or not A or not M then return end for y,Z in pairs(s:GetChildren())do if Z:FindFirstChild("\083\117\115\112\101\110\115\105\111\110")then Z.Suspension.BG.CFrame=A.CFrame*CFrame.Angles(-math.pi/2,-math.rad(M.SuspensionAngle.Value),0)end end end UpdateWheels()for y,Z in pairs(s:GetChildren())do if Z:FindFirstChild("\083\117\115\112\101\110\115\105\111\110")then local y=Z:FindFirstChild("\083\116\101\101\114")~=nil if y then Z.Suspension.BG.maxTorque=Vector3.new(0,3000000,0)else Z.Suspension.BG.maxTorque=Vector3.new(0,9000000,0)end end end Z.Heartbeat:connect(UpdateWheels)
-------------------------------------------------------------------------------------------------------------------------------------------------------
local P = script.Parent
local Run = game:GetService("RunService")
local wheels = P.System.Wheels
local seat = P.System.VehicleSeat
local vars = seat.ATSVariables

local exclude = { "Wheels" }

function WeldPair(x, y)
	local weld = Instance.new("Weld", x)
	weld.Part0 = x
	weld.Part1 = y
	weld.C1 = y.CFrame:toObjectSpace(x.CFrame)
end

function WeldAll(model, main) -- model can be anything
	for i, v in pairs(exclude) do
		if model.Name == v then
			return
		end
	end
	if model:IsA("BasePart") then
		WeldPair(main, model)
	end
	for _, v in pairs(model:GetChildren()) do
		WeldAll(v, main)
	end
end

function UnanchorAll(model) -- model can be anything
	if model:IsA("BasePart") then
		model.Anchored = false
	end
	for _, v in pairs(model:GetChildren()) do
		UnanchorAll(v)
	end
end

function Joint(x, y, c0, c1, type)
	local W = Instance.new(type, x)
	W.Part0 = x
	W.Part1 = y
	W.C0 = c0
	W.C1 = c1
	return W
end

function CreateWheelPart()
	local p = Instance.new("Part")
	p.FormFactor = Enum.FormFactor.Custom
	p.Transparency = 1
	p.Anchored = true
	p.CanCollide = false
	p.TopSurface = Enum.SurfaceType.Unjoinable
	p.BottomSurface = Enum.SurfaceType.Unjoinable
	p.LeftSurface = Enum.SurfaceType.Unjoinable
	p.RightSurface = Enum.SurfaceType.Unjoinable
	p.FrontSurface = Enum.SurfaceType.Unjoinable
	p.BackSurface = Enum.SurfaceType.Unjoinable
	return p
end

for i, v in pairs(wheels:GetChildren()) do
	if v:IsA("BasePart") then
		local diameter = v.Size.x
		local M = Instance.new("Model", wheels)
		M.Name = v.Name
		v.Parent = M
		v.Name = "Wheel"
		for j, w in pairs(v:GetChildren()) do
			if w:IsA("BasePart") then
				w.Parent = M
				WeldPair(v, w)
				w.CanCollide = false
			end
		end
		if v:FindFirstChild("Steer") then
			local A = CreateWheelPart()
			A.Name = "Steer"
			A.Size = Vector3.new(diameter, 1, 0.6)
			A.CFrame = v.CFrame
			A.Parent = M
		end
		local B = CreateWheelPart()
		B.Name = "Suspension"
		B.Size = Vector3.new(0.8, 1, diameter)
		B.CFrame = v.CFrame * CFrame.new(-1, 0, 0)
		B.Parent = M
		local BB = Instance.new("BodyGyro")
		BB.Name = "BG"
		if v:FindFirstChild("Steer") then
			BB.D = vars.SuspensionDampeningFront.Value
		else
			BB.D = vars.SuspensionDampeningRear.Value
		end
		BB.P = vars.SuspensionPower.Value
		BB.maxTorque = Vector3.new()
		BB.Parent = B
		local C = CreateWheelPart()
		C.Name = "BodyHinge"
		C.Size = Vector3.new(0.8, 1, diameter)
		C.CFrame = v.CFrame * CFrame.new(1, 0, 0)
		C.Parent = M
		if v:FindFirstChild("Powered") then
			local CC = Instance.new("BodyAngularVelocity")
			CC.Name = "BAV"
			CC.angularvelocity = Vector3.new()
			CC.maxTorque = Vector3.new(20000, 0, 20000)
			CC.P = 800
			CC.Parent = v
		end
	end
end

for i, v in pairs(wheels:GetChildren()) do
	if (v:FindFirstChild("Wheel")) and (v:FindFirstChild("Suspension")) then
		local isSteeringWheel = (v:FindFirstChild("Steer") ~= nil)
		if v:FindFirstChild("Mesh") then
			WeldAll(v.Mesh, v.Wheel)
		end
		if isSteeringWheel then
			Joint(
				v.Wheel,
				v.Steer,
				CFrame.Angles(math.pi / 2, 0, 0),
				v.Steer.CFrame:toObjectSpace(v.Wheel.CFrame) * CFrame.Angles(math.pi / 2, 0, 0),
				"Rotate"
			)
			local M = Joint(
				v.Steer,
				v.Suspension,
				CFrame.Angles(math.pi, 0, 0),
				v.Suspension.CFrame:toObjectSpace(v.Steer.CFrame) * CFrame.Angles(math.pi, 0, 0),
				"Motor"
			)
			M.MaxVelocity = 0.03
		else
			Joint(
				v.Wheel,
				v.Suspension,
				CFrame.Angles(math.pi / 2, 0, 0),
				v.Suspension.CFrame:toObjectSpace(v.Wheel.CFrame) * CFrame.Angles(math.pi / 2, 0, 0),
				"Rotate"
			)
		end
		Joint(
			v.Suspension,
			v.BodyHinge,
			CFrame.new(Vector3.new(2, 0, 0)) * CFrame.Angles(math.pi / 2, 0, 0),
			v.BodyHinge.CFrame:toObjectSpace(v.Suspension.CFrame)
				* CFrame.new(Vector3.new(2, 0, 0))
				* CFrame.Angles(math.pi / 2, 0, 0),
			"Rotate"
		)
		Joint(v.BodyHinge, seat, CFrame.new(), seat.CFrame:toObjectSpace(v.BodyHinge.CFrame), "Weld")
	else
		print("Invalid wheel: Missing Wheel or Suspension part!")
	end
end

WeldAll(P, seat)

wait(0.5)

UnanchorAll(P)

function UpdateWheels()
	if (not wheels) or not seat or not vars then
		return
	end
	for i, v in pairs(wheels:GetChildren()) do
		if v:FindFirstChild("Suspension") then
			v.Suspension.BG.CFrame = seat.CFrame * CFrame.Angles(-math.pi / 2, -math.rad(vars.SuspensionAngle.Value), 0)
		end
	end
end

UpdateWheels()
for i, v in pairs(wheels:GetChildren()) do
	if v:FindFirstChild("Suspension") then
		local isSteeringWheel = (v:FindFirstChild("Steer") ~= nil)
		if isSteeringWheel then
			v.Suspension.BG.maxTorque = Vector3.new(0, 3000000, 0)
		else
			v.Suspension.BG.maxTorque = Vector3.new(0, 9000000, 0)
		end
	end
end

Run.Heartbeat:connect(UpdateWheels)
