local v=script.Parent local S=game:GetService("\082\117\110\083\101\114\118\105\099\101")local f=v.System.Wheels local H=v.System.VehicleSeat local u=H.ATSVariables local q={"\087\104\101\101\108\115"}function WeldPair(v,S)local f=Instance.new("\087\101\108\100",v)f.Part0=v f.Part1=S f.C1=S.CFrame:toObjectSpace(v.CFrame)end function WeldAll(v,S)for S,f in pairs(q)do if v.Name==f then return end end if v:IsA("\066\097\115\101\080\097\114\116")then WeldPair(S,v)end for v,f in pairs(v:GetChildren())do WeldAll(f,S)end end function UnanchorAll(v)if v:IsA("\066\097\115\101\080\097\114\116")then v.Anchored=false end for v,S in pairs(v:GetChildren())do UnanchorAll(S)end end function Joint(v,S,f,H,u)local q=Instance.new(u,v)q.Part0=v q.Part1=S q.C0=f q.C1=H return q end function CreateWheelPart()local v=Instance.new("\080\097\114\116")v.FormFactor=Enum.FormFactor.Custom v.Transparency=1 v.Anchored=true v.CanCollide=false v.TopSurface=Enum.SurfaceType.Unjoinable v.BottomSurface=Enum.SurfaceType.Unjoinable v.LeftSurface=Enum.SurfaceType.Unjoinable v.RightSurface=Enum.SurfaceType.Unjoinable v.FrontSurface=Enum.SurfaceType.Unjoinable v.BackSurface=Enum.SurfaceType.Unjoinable return v end for v,S in pairs(f:GetChildren())do if S:IsA("\066\097\115\101\080\097\114\116")then local v=S.Size.x local H=Instance.new("\077\111\100\101\108",f)H.Name=S.Name S.Parent=H S.Name="\087\104\101\101\108"for v,f in pairs(S:GetChildren())do if f:IsA("\066\097\115\101\080\097\114\116")then f.Parent=H WeldPair(S,f)f.CanCollide=false end end if S:FindFirstChild("\083\116\101\101\114")then local f=CreateWheelPart()f.Name="\083\116\101\101\114"f.Size=Vector3.new(v,1,.6)f.CFrame=S.CFrame f.Parent=H end local q=CreateWheelPart()q.Name="\083\117\115\112\101\110\115\105\111\110"q.Size=Vector3.new(.8,1,v)q.CFrame=S.CFrame*CFrame.new(-1,0,0)q.Parent=H local p=Instance.new("\066\111\100\121\071\121\114\111")p.Name="\066\071"if S:FindFirstChild("\083\116\101\101\114")then p.D=u.SuspensionDampeningFront.Value else p.D=u.SuspensionDampeningRear.Value end p.P=u.SuspensionPower.Value p.maxTorque=Vector3.new()p.Parent=q local c=CreateWheelPart()c.Name="\066\111\100\121\072\105\110\103\101"c.Size=Vector3.new(.8,1,v)c.CFrame=S.CFrame*CFrame.new(1,0,0)c.Parent=H if S:FindFirstChild("\080\111\119\101\114\101\100")then local v=Instance.new("\066\111\100\121\065\110\103\117\108\097\114\086\101\108\111\099\105\116\121")v.Name="\066\065\086"v.angularvelocity=Vector3.new()v.maxTorque=Vector3.new(20000,0,20000)v.P=800 v.Parent=S end end end for v,S in pairs(f:GetChildren())do if S:FindFirstChild("\087\104\101\101\108")and S:FindFirstChild("\083\117\115\112\101\110\115\105\111\110")then local v=S:FindFirstChild("\083\116\101\101\114")~=nil if S:FindFirstChild("\077\101\115\104")then WeldAll(S.Mesh,S.Wheel)end if v then Joint(S.Wheel,S.Steer,CFrame.Angles(math.pi/2,0,0),S.Steer.CFrame:toObjectSpace(S.Wheel.CFrame)*CFrame.Angles(math.pi/2,0,0),"\082\111\116\097\116\101")local v=Joint(S.Steer,S.Suspension,CFrame.Angles(math.pi,0,0),S.Suspension.CFrame:toObjectSpace(S.Steer.CFrame)*CFrame.Angles(math.pi,0,0),"\077\111\116\111\114")v.MaxVelocity=.03 else Joint(S.Wheel,S.Suspension,CFrame.Angles(math.pi/2,0,0),S.Suspension.CFrame:toObjectSpace(S.Wheel.CFrame)*CFrame.Angles(math.pi/2,0,0),"\082\111\116\097\116\101")end Joint(S.Suspension,S.BodyHinge,CFrame.new(Vector3.new(2,0,0))*CFrame.Angles(math.pi/2,0,0),(S.BodyHinge.CFrame:toObjectSpace(S.Suspension.CFrame)*CFrame.new(Vector3.new(2,0,0)))*CFrame.Angles(math.pi/2,0,0),"\082\111\116\097\116\101")Joint(S.BodyHinge,H,CFrame.new(),H.CFrame:toObjectSpace(S.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(v,H)wait(.5)UnanchorAll(v)function UpdateWheels()if not f or not H or not u then return end for v,S in pairs(f:GetChildren())do if S:FindFirstChild("\083\117\115\112\101\110\115\105\111\110")then S.Suspension.BG.CFrame=H.CFrame*CFrame.Angles(-math.pi/2,-math.rad(u.SuspensionAngle.Value),0)end end end UpdateWheels()for v,S in pairs(f:GetChildren())do if S:FindFirstChild("\083\117\115\112\101\110\115\105\111\110")then local v=S:FindFirstChild("\083\116\101\101\114")~=nil if v then S.Suspension.BG.maxTorque=Vector3.new(0,3000000,0)else S.Suspension.BG.maxTorque=Vector3.new(0,9000000,0)end end end S.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)
