How to cancel a loop that adds a value?

Well, basically the following adds a number from the numberValue for a progress bar. The script works by holding down a defined key and releasing the key starts subtracting the value from the numberValue. The problem here is that the inputBegan function keeps executing despite releasing it.

local UserInputService = game:GetService("UserInputService")

local numValue = script.Parent:FindFirstChild("numValue")

local Enabled = false

UserInputService.InputBegan:Connect(function(key)
	if (key.KeyCode == Enum.KeyCode.E) then
		if Enabled == false then
			while Enabled == false and wait(0.01) do
				numValue.Value = numValue.Value + 1
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(key) -- Detect when key is lifted
	if (key.KeyCode == Enum.KeyCode.E) then
		Enabled = false
		while Enabled == true and wait(0.01) do
			numValue.Value = numValue.Value - 1
		end
	end
end)

You’re never setting the Enabled variable to true. You should also keep this loop outside of the Input events if it’s meant to be running all the time. Try something like this:

local UserInputService = game:GetService("UserInputService")
local numValue = script.Parent:FindFirstChild("numValue")
local Enabled = false

UserInputService.InputBegan:Connect(function(key)
	if (key.KeyCode == Enum.KeyCode.E) then
		Enabled = true
	end
end)

UserInputService.InputEnded:Connect(function(key) -- Detect when key is lifted
	if (key.KeyCode == Enum.KeyCode.E) then
		Enabled = false
	end
end)

while(wait(0.01)) do
	local change = (Enabled and 1) or -1
	numValue.Value += change
end