local function F(F,T,r)local u=Instance.new("\087\101\108\100",F)u.Name=r u.C0=F.CFrame:inverse()*T.CFrame u.Part0=F u.Part1=T end ToggleEngineEvent.OnServerEvent:Connect(function(F)if not EngineOn and BoatControlActivated then EngineOn=true EngineOnVal.Value=true ToggleEngineEvent:FireClient(F,EngineOn)MainPart.Main:Play()elseif EngineOn then ReducingSpeed=true EngineOn=false EngineOnVal.Value=false ToggleEngineEvent:FireClient(F,EngineOn)if MainPart.Horn.isPlaying then MainPart.Horn:Pause()end MainPart.Main:Pause()for F=Thrust,1,-1 do Thrust=Thrust-1 ThrustVal.Value=Thrust local T,r,u=MainPart.BodyGyro.CFrame:ToOrientation()if not(T==0)then MainPart.BodyGyro.CFrame=CFrame.fromOrientation((math.abs(T)/T)*math.rad(Thrust/10),r,math.rad(Thrust/25))else MainPart.BodyGyro.CFrame=CFrame.fromOrientation(T,r,math.rad(Thrust/elevationAngle))end if EngineOn then break end wait(.05)end MainPart.BodyVelocity.Velocity=Vector3.new(0,0,0)end ReducingSpeed=false end)SteerEvent.OnServerEvent:Connect(function(F,T)if T==1 and EngineOn then if not ReducingSpeed then if Thrust>=0 then TurnAngle=-turnSpeed elseif Thrust<0 then TurnAngle=turnSpeed end end local F=CFrame.fromEulerAnglesXYZ(0,TurnAngle,0)MainPart.BodyGyro.CFrame=F*(MainPart.BodyGyro.CFrame-MainPart.BodyGyro.CFrame.p)+MainPart.BodyGyro.CFrame.p local T,r,u=MainPart.BodyGyro.CFrame:ToOrientation()MainPart.BodyGyro.CFrame=CFrame.fromOrientation(math.rad(Thrust/turnAngleDiv),r,u)elseif T==-1 and EngineOn then if not ReducingSpeed then if Thrust>=0 then TurnAngle=turnSpeed elseif Thrust<0 then TurnAngle=-turnSpeed end end local F=CFrame.fromEulerAnglesXYZ(0,TurnAngle,0)MainPart.BodyGyro.CFrame=F*(MainPart.BodyGyro.CFrame-MainPart.BodyGyro.CFrame.p)+MainPart.BodyGyro.CFrame.p local T,r,u=MainPart.BodyGyro.CFrame:ToOrientation()MainPart.BodyGyro.CFrame=CFrame.fromOrientation(math.rad(-Thrust/turnAngleDiv),r,u)elseif T==0 and EngineOn then TurnAngle=0 local F,T,r=MainPart.BodyGyro.CFrame:ToOrientation()MainPart.BodyGyro.CFrame=CFrame.fromOrientation(0,T,r)end end)ThrottleEvent.OnServerEvent:Connect(function(F,T)if neutral then neutral=false GearShiftSound:Play()end if T==0 then neutral=true end Thrust=T local r,u,V=MainPart.BodyGyro.CFrame:ToOrientation()MainPart.BodyGyro.CFrame=CFrame.fromOrientation(r,u,math.rad(Thrust/100))ThrustVal.Value=Thrust end)ToggleSoundEvent.OnServerEvent:Connect(function(F,T,r)if r then T:Play()elseif not r then T:Pause()end end)ThrustersEvent.OnServerEvent:Connect(function(F,T)if EngineOn and THRUSTERS_ENABLED then SideThrusterSpeed=T end end)while wait()do if EngineOn or ReducingSpeed then MainPart.BodyVelocity.Velocity=MainPart.CFrame.rightVector*(Thrust/speedDiv)+MainPart.CFrame.lookVector*SideThrusterSpeed MainPart.Main.PlaybackSpeed=1+math.abs(Thrust/150)end end
--------------------------------------

local function weldBetween(a, b, weldName)
	local weld = Instance.new("Weld", a)
	weld.Name = weldName
	weld.C0 = a.CFrame:inverse() * b.CFrame
	weld.Part0 = a
	weld.Part1 = b
end

ToggleEngineEvent.OnServerEvent:Connect(function(plr) -- Toggle Engine
	if not EngineOn and BoatControlActivated then
		EngineOn = true
		EngineOnVal.Value = true
		ToggleEngineEvent:FireClient(plr, EngineOn)
		MainPart.Main:Play()
	elseif EngineOn then
		ReducingSpeed = true
		EngineOn = false
		EngineOnVal.Value = false
		ToggleEngineEvent:FireClient(plr, EngineOn)
		if MainPart.Horn.isPlaying then
			MainPart.Horn:Pause()
		end
		MainPart.Main:Pause()
		for i = Thrust, 1, -1 do
			Thrust = Thrust - 1
			ThrustVal.Value = Thrust
			local x, y, z = MainPart.BodyGyro.CFrame:ToOrientation()
			if not (x == 0) then
				MainPart.BodyGyro.CFrame =
					CFrame.fromOrientation((math.abs(x) / x) * (math.rad(Thrust / 10)), y, math.rad(Thrust / 25))
			else
				MainPart.BodyGyro.CFrame = CFrame.fromOrientation(x, y, math.rad(Thrust / elevationAngle))
			end
			if EngineOn then
				break
			end
			wait(0.05)
		end
		MainPart.BodyVelocity.Velocity = Vector3.new(0, 0, 0)
	end
	ReducingSpeed = false
end)

SteerEvent.OnServerEvent:Connect(function(plr, steer)
	if steer == 1 and EngineOn then
		if not ReducingSpeed then
			if Thrust >= 0 then
				TurnAngle = -turnSpeed
			elseif Thrust < 0 then
				TurnAngle = turnSpeed
			end
		end
		local rot = CFrame.fromEulerAnglesXYZ(0, TurnAngle, 0)
		MainPart.BodyGyro.CFrame = (rot * (MainPart.BodyGyro.CFrame - MainPart.BodyGyro.CFrame.p))
			+ MainPart.BodyGyro.CFrame.p
		local x, y, z = MainPart.BodyGyro.CFrame:ToOrientation()
		MainPart.BodyGyro.CFrame = CFrame.fromOrientation(math.rad(Thrust / turnAngleDiv), y, z)
	elseif steer == -1 and EngineOn then
		if not ReducingSpeed then
			if Thrust >= 0 then
				TurnAngle = turnSpeed
			elseif Thrust < 0 then
				TurnAngle = -turnSpeed
			end
		end
		local rot = CFrame.fromEulerAnglesXYZ(0, TurnAngle, 0)
		MainPart.BodyGyro.CFrame = (rot * (MainPart.BodyGyro.CFrame - MainPart.BodyGyro.CFrame.p))
			+ MainPart.BodyGyro.CFrame.p
		local x, y, z = MainPart.BodyGyro.CFrame:ToOrientation()
		MainPart.BodyGyro.CFrame = CFrame.fromOrientation(math.rad(-Thrust / turnAngleDiv), y, z)
	elseif steer == 0 and EngineOn then
		TurnAngle = 0
		local x, y, z = MainPart.BodyGyro.CFrame:ToOrientation()
		MainPart.BodyGyro.CFrame = CFrame.fromOrientation(0, y, z)
	end
end)

ThrottleEvent.OnServerEvent:Connect(function(plr, throttle)
	if neutral then
		neutral = false
		GearShiftSound:Play()
	end
	if throttle == 0 then
		neutral = true
	end
	Thrust = throttle
	local x, y, z = MainPart.BodyGyro.CFrame:ToOrientation()
	MainPart.BodyGyro.CFrame = CFrame.fromOrientation(x, y, math.rad(Thrust / 100))
	ThrustVal.Value = Thrust
end)

ToggleSoundEvent.OnServerEvent:Connect(function(plr, sound, val)
	if val then
		sound:Play()
	elseif not val then
		sound:Pause()
	end
end)

ThrustersEvent.OnServerEvent:Connect(function(plr, speed)
	if EngineOn and THRUSTERS_ENABLED then
		SideThrusterSpeed = speed
	end
end)

while wait() do
	if EngineOn or ReducingSpeed then
		MainPart.BodyVelocity.Velocity = (MainPart.CFrame.rightVector * (Thrust / speedDiv))
			+ (MainPart.CFrame.lookVector * SideThrusterSpeed)
		MainPart.Main.PlaybackSpeed = 1 + math.abs(Thrust / 150)
	end
end
