Animation Still Plays After Boolean Is False

Hello, I’m currently working on a boolean script, and I’ve ran into a problem. When the boolean is set the true, although everything is working as intended. The player doesn’t start off in the animation when standing still. All the major problems accrue when the boolean is set to false. If I do set the boolean to false and the animation was previously playing then yeah, It’ll stop the animation. But if I start walking and stand still the animation plays even though the boolean is false. Why is this happening?

Code:

function QBMode(Boolean)
	if Boolean == true then
		Character.CharacterInformation.InQBMode.Value = true
		LockModule:Mode(true)
		Humanoid.Running:Connect(function(Speed)
			if Speed == 0 and not InQB then
				InQB = true
				local Moved = false
				-- Anims --
				if not Moved then
					QBAnimation:Play()
				end
				Humanoid.Running:Wait()
				Moved = true
				QBAnimation:Stop()
				InQB = false
			end
		end)
	elseif Boolean == false then
		Character.CharacterInformation.InQBMode.Value = false
		InQB = false
		LockModule:Mode(false)
		QBAnimation:Stop()
	end
end
--

Maybe try attaching the Humanoid.Running function to a variable, and have the function disconnect via the variable when the boolean is false.
Something like:

local function = Humanoid.Running:Connect(function(Speed)
    -- whatever you need here
end)

-- fire the qbmode function with the boolean as false
-- if statement for true goes here

elseif Boolean == false then
      function:Disconnect() --[[ basically stops it from playing the animation until the 
connection is put back into the variable, which is when qbmode(true) is fired]]
end

I’m confused cause I get a red underline.

The code I provided shouldn’t be copied verbatim, but is just an example as to what you should do.