Break out of a loop that is inside a function but calling the break outside of a loop

So i want the line to stop moving to the left when the tool is clicked again but it’s telling me that func1 is nil I wonder how can i stop the for loop inside of a function from running

local PitchEvents = game.ReplicatedStorage.PitchEvents
local Plr = game.Players.LocalPlayer
local Speed = 0.03

local PowerBar = script.Parent:WaitForChild("PowerBar")

local DidNotClick = false
local function MoveLineToLeft()
	
	
	
	PowerBar.Line.Visible = true

	for i = 1,0,-Speed do
		PowerBar.Line.Position = UDim2.new(i,0,0,0)
		wait()
		
		if PowerBar.Line.Position.X.Scale <= 0.247 then
			PowerBar.BlackCover.Size = UDim2.new(i,0,1,0)
		end
		
	end
	
	PowerBar.Line.Position = UDim2.new(0,0,0,0)
	PowerBar.BlackCover.Size = UDim2.new(0,0,1,0)
	
	DidNotClick = true


end




local func1 
PitchEvents.Click.OnClientEvent:Connect(function(clicks)



	if clicks == 1 then
		func1 = MoveLineToLeft()
	

	elseif clicks == 2 then

		func1:Disconnect()
	end

end)

error msg

Players.sktvladimir535.PlayerGui.PitchingGui.LocalScript:46: attempt to index nil with 'Disconnect'

MoveLinesToLeft does not relate to or return an RBXScriptConnection

For this you’re gonna need a simple modification.

Try this:

local PitchEvents = game.ReplicatedStorage.PitchEvents
local Plr = game.Players.LocalPlayer
local Speed = 0.03
local Break = false

local PowerBar = script.Parent:WaitForChild("PowerBar")

local DidNotClick = false
local function MoveLineToLeft()
	
	
	
	PowerBar.Line.Visible = true

	for i = 1,0,-Speed do
    -- Now we add a simple check
    if Break then
      Break = false
      break
    end

		PowerBar.Line.Position = UDim2.new(i,0,0,0)
		task.wait() -- I don't recommend wait(), use task.wait()
		
		if PowerBar.Line.Position.X.Scale <= 0.247 then
			PowerBar.BlackCover.Size = UDim2.new(i,0,1,0)
		end
		
	end
	
	PowerBar.Line.Position = UDim2.new(0,0,0,0)
	PowerBar.BlackCover.Size = UDim2.new(0,0,1,0)
	
	DidNotClick = true


end




local func1 
PitchEvents.Click.OnClientEvent:Connect(function(clicks)



	if clicks == 1 then
		func1 = MoveLineToLeft()
	

	elseif clicks == 2 then

		Break = true
	end

end)