--------------------------------------



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