local function a(a,Z,T)local D=Instance.new("\087\101\108\100",a)D.Name=T D.C0=a.CFrame:inverse()*Z.CFrame D.Part0=a D.Part1=Z end ToggleEngineEvent.OnServerEvent:Connect(function(a)if not EngineOn and BoatControlActivated then EngineOn=true EngineOnVal.Value=true ToggleEngineEvent:FireClient(a,EngineOn)MainPart.Main:Play()elseif EngineOn then ReducingSpeed=true EngineOn=false EngineOnVal.Value=false ToggleEngineEvent:FireClient(a,EngineOn)if MainPart.Horn.isPlaying then MainPart.Horn:Pause()end MainPart.Main:Pause()for a=Thrust,1,-1 do Thrust=Thrust-1 ThrustVal.Value=Thrust local Z,T,D=MainPart.BodyGyro.CFrame:ToOrientation()if not(Z==0)then MainPart.BodyGyro.CFrame=CFrame.fromOrientation((math.abs(Z)/Z)*math.rad(Thrust/10),T,math.rad(Thrust/25))else MainPart.BodyGyro.CFrame=CFrame.fromOrientation(Z,T,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(a,Z)if Z==1 and EngineOn then if not ReducingSpeed then if Thrust>=0 then TurnAngle=-turnSpeed elseif Thrust<0 then TurnAngle=turnSpeed end end local a=CFrame.fromEulerAnglesXYZ(0,TurnAngle,0)MainPart.BodyGyro.CFrame=a*(MainPart.BodyGyro.CFrame-MainPart.BodyGyro.CFrame.p)+MainPart.BodyGyro.CFrame.p local Z,T,D=MainPart.BodyGyro.CFrame:ToOrientation()MainPart.BodyGyro.CFrame=CFrame.fromOrientation(math.rad(Thrust/turnAngleDiv),T,D)elseif Z==-1 and EngineOn then if not ReducingSpeed then if Thrust>=0 then TurnAngle=turnSpeed elseif Thrust<0 then TurnAngle=-turnSpeed end end local a=CFrame.fromEulerAnglesXYZ(0,TurnAngle,0)MainPart.BodyGyro.CFrame=a*(MainPart.BodyGyro.CFrame-MainPart.BodyGyro.CFrame.p)+MainPart.BodyGyro.CFrame.p local Z,T,D=MainPart.BodyGyro.CFrame:ToOrientation()MainPart.BodyGyro.CFrame=CFrame.fromOrientation(math.rad(-Thrust/turnAngleDiv),T,D)elseif Z==0 and EngineOn then TurnAngle=0 local a,Z,T=MainPart.BodyGyro.CFrame:ToOrientation()MainPart.BodyGyro.CFrame=CFrame.fromOrientation(0,Z,T)end end)ThrottleEvent.OnServerEvent:Connect(function(a,Z)if neutral then neutral=false GearShiftSound:Play()end if Z==0 then neutral=true end Thrust=Z local T,D,U=MainPart.BodyGyro.CFrame:ToOrientation()MainPart.BodyGyro.CFrame=CFrame.fromOrientation(T,D,math.rad(Thrust/100))ThrustVal.Value=Thrust end)ToggleSoundEvent.OnServerEvent:Connect(function(a,Z,T)if T then Z:Play()elseif not T then Z:Pause()end end)ThrustersEvent.OnServerEvent:Connect(function(a,Z)if EngineOn and THRUSTERS_ENABLED then SideThrusterSpeed=Z 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(.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


