local b=script.Parent local S=b.Configurations local G=require(script.RaycastModule)local g=0 for b,S in pairs(b:GetChildren())do if S:IsA("\066\097\115\101\080\097\114\116")then g=g+S:GetMass()*196.2 end end local f=Instance.new("\066\111\100\121\080\111\115\105\116\105\111\110",b.Chassis)f.MaxForce=Vector3.new()local r=Instance.new("\066\111\100\121\071\121\114\111",b.Chassis)r.MaxTorque=Vector3.new()local function d(g)local f,r=G.new(g.Position,g.CFrame:vectorToWorldSpace(Vector3.new(0,-1,0))*S.Height.Value)local d=(r-g.Position).magnitude local a=g:FindFirstChild("\087\104\101\101\108\087\101\108\100")a.C0=CFrame.new(0,-math.min(d,S.Height.Value*.8)+a.Part1.Size.Y/2,0)local K=b.Chassis.CFrame:inverse()*g.CFrame local q=b.Chassis.CFrame:vectorToObjectSpace(b.Chassis.Velocity)if K.Z<0 then local S=1 if q.Z>0 then S=-1 end a.C0=a.C0*CFrame.Angles(0,(b.Chassis.RotVelocity.Y/2)*S,0)end if f and g.Velocity.magnitude>=5 then a.Part1.ParticleEmitter.Enabled=true else a.Part1.ParticleEmitter.Enabled=false end end b.DriveSeat.Changed:connect(function(S)if S=="\079\099\099\117\112\097\110\116"then if b.DriveSeat.Occupant then local S=game.Players:GetPlayerFromCharacter(b.DriveSeat.Occupant.Parent)if S then b.DriveSeat:SetNetworkOwner(S)local G=script.LocalCarScript:Clone()G.Parent=S.PlayerGui G.Car.Value=b G.Disabled=false end else b.DriveSeat:SetNetworkOwnershipAuto()end end end)spawn(function()while true do(game:GetService("\082\117\110\083\101\114\118\105\099\101")).Stepped:wait()for b,S in pairs(b:GetChildren())do if S.Name=="\084\104\114\117\115\116\101\114"then d(S)end end if b.DriveSeat.Occupant then f.MaxForce=Vector3.new()r.MaxTorque=Vector3.new()else local d,a,K=G.new(b.Chassis.Position,b.Chassis.CFrame:vectorToWorldSpace(Vector3.new(0,-1,0))*S.Height.Value)if d and d.CanCollide then f.MaxForce=Vector3.new(g/5,math.huge,g/5)f.Position=(CFrame.new(a,a+K)*CFrame.new(0,0,-S.Height.Value+.5)).p r.MaxTorque=Vector3.new(math.huge,0,math.huge)r.CFrame=CFrame.new(a,a+K)*CFrame.Angles(-math.pi/2,0,0)else f.MaxForce=Vector3.new()r.MaxTorque=Vector3.new()end end end end)


local car = script.Parent
local stats = car.Configurations
local Raycast = require(script.RaycastModule)

local mass = 0

for i, v in pairs(car:GetChildren()) do
	if v:IsA("BasePart") then
		mass = mass + (v:GetMass() * 196.2)
	end
end

local bodyPosition = Instance.new("BodyPosition", car.Chassis)
bodyPosition.MaxForce = Vector3.new()
local bodyGyro = Instance.new("BodyGyro", car.Chassis)
bodyGyro.MaxTorque = Vector3.new()

local function UpdateThruster(thruster)
	-- Raycasting
	local hit, position = Raycast.new(thruster.Position, thruster.CFrame:vectorToWorldSpace(Vector3.new(0, -1, 0)) * stats.Height.Value) --game.Workspace:FindPartOnRay(ray, car)
	local thrusterHeight = (position - thruster.Position).magnitude

	-- Wheel
	local wheelWeld = thruster:FindFirstChild("WheelWeld")
	wheelWeld.C0 = CFrame.new(0, -math.min(thrusterHeight, stats.Height.Value * 0.8) + (wheelWeld.Part1.Size.Y / 2), 0)
	-- Wheel turning
	local offset = car.Chassis.CFrame:inverse() * thruster.CFrame
	local speed = car.Chassis.CFrame:vectorToObjectSpace(car.Chassis.Velocity)
	if offset.Z < 0 then
		local direction = 1
		if speed.Z > 0 then
			direction = -1
		end
		wheelWeld.C0 = wheelWeld.C0 * CFrame.Angles(0, (car.Chassis.RotVelocity.Y / 2) * direction, 0)
	end

	-- Particles
	if hit and thruster.Velocity.magnitude >= 5 then
		wheelWeld.Part1.ParticleEmitter.Enabled = true
	else
		wheelWeld.Part1.ParticleEmitter.Enabled = false
	end
end

car.DriveSeat.Changed:connect(function(property)
	if property == "Occupant" then
		if car.DriveSeat.Occupant then
			local player = game.Players:GetPlayerFromCharacter(car.DriveSeat.Occupant.Parent)
			if player then
				car.DriveSeat:SetNetworkOwner(player)
				local localCarScript = script.LocalCarScript:Clone()
				localCarScript.Parent = player.PlayerGui
				localCarScript.Car.Value = car
				localCarScript.Disabled = false
			end
		else
			car.DriveSeat:SetNetworkOwnershipAuto()
		end
	end
end)

spawn(function()
	while true do
		game:GetService("RunService").Stepped:wait()
		for i, part in pairs(car:GetChildren()) do
			if part.Name == "Thruster" then
				UpdateThruster(part)
			end
		end
		if car.DriveSeat.Occupant then
			bodyPosition.MaxForce = Vector3.new()
			bodyGyro.MaxTorque = Vector3.new()
		else
			local hit, position, normal = Raycast.new(car.Chassis.Position, car.Chassis.CFrame:vectorToWorldSpace(Vector3.new(0, -1, 0)) * stats.Height.Value)
			if hit and hit.CanCollide then
				bodyPosition.MaxForce = Vector3.new(mass / 5, math.huge, mass / 5)
				bodyPosition.Position = (CFrame.new(position, position + normal) * CFrame.new(0, 0, -stats.Height.Value + 0.5)).p
				bodyGyro.MaxTorque = Vector3.new(math.huge, 0, math.huge)
				bodyGyro.CFrame = CFrame.new(position, position + normal) * CFrame.Angles(-math.pi/2, 0, 0)
			else
				bodyPosition.MaxForce = Vector3.new()
				bodyGyro.MaxTorque = Vector3.new()
			end
		end
	end
end)
