local function n(n)local M,h=Raycast.new(n.Position,n.CFrame:vectorToWorldSpace(Vector3.new(0,-1,0))*stats.Height.Value)local P=(h-n.Position).magnitude local L=n:FindFirstChild("\087\104\101\101\108\087\101\108\100")L.C0=CFrame.new(0,-math.min(P,stats.Height.Value*.8)+L.Part1.Size.Y/2,0)local V=car.Chassis.CFrame:inverse()*n.CFrame local f=car.Chassis.CFrame:vectorToObjectSpace(car.Chassis.Velocity)if V.Z<0 then local n=1 if f.Z>0 then n=-1 end L.C0=L.C0*CFrame.Angles(0,(car.Chassis.RotVelocity.Y/2)*n,0)end if M and n.Velocity.magnitude>=5 then L.Part1.ParticleEmitter.Enabled=true else L.Part1.ParticleEmitter.Enabled=false end end car.Event.OnServerEvent:Connect(function(n)if n and n.Character then if(n.Character.HumanoidRootPart.Position-car.DriveSeat.Position).magnitude<=car.Configurations.Range.Value then if not HasDriver then car.DriveSeat:Sit(n.Character.Humanoid)end end end end)car.DriveSeat.Changed:connect(function(n)if n=="\079\099\099\117\112\097\110\116"then if car.DriveSeat.Occupant then car.EngineBlock.Running.Pitch=1 car.EngineBlock.Running:Play()local n=game.Players:GetPlayerFromCharacter(car.DriveSeat.Occupant.Parent)if n then HasDriver=true car.DriveSeat:SetNetworkOwner(n)local M=script.LocalCarScript:Clone()M.Parent=n.PlayerGui M.Car.Value=car M.Disabled=false end else HasDriver=false car.EngineBlock.Running:Stop()end end end)


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.Event.OnServerEvent:Connect(function(Player)
	if Player and Player.Character then
		if (Player.Character.HumanoidRootPart.Position-car.DriveSeat.Position).magnitude <= car.Configurations.Range.Value then
			if not HasDriver then
				car.DriveSeat:Sit(Player.Character.Humanoid)
			end
		end
	end
end)

car.DriveSeat.Changed:connect(function(property)
	if property == "Occupant" then
		if car.DriveSeat.Occupant then
			car.EngineBlock.Running.Pitch = 1
			car.EngineBlock.Running:Play()
			local player = game.Players:GetPlayerFromCharacter(car.DriveSeat.Occupant.Parent)
			if player then
				HasDriver = true
				car.DriveSeat:SetNetworkOwner(player)
				local localCarScript = script.LocalCarScript:Clone()
				localCarScript.Parent = player.PlayerGui
				localCarScript.Car.Value = car
				localCarScript.Disabled = false
			end
		else
			HasDriver = false
			car.EngineBlock.Running:Stop()
		end
	end
end)
