Repeat making my frame transparent faster

Ok so I have a flashlight script and a battery thing but when I toggle the flashlight on an off continuously then turn it on the frame becomes transparent even faster

local function drainBattery(bool)
	if bool == true then
		if lp.PlayerGui.FlashLight.BatteryF.Bar:FindFirstChild("Bar"..bar).BackgroundTransparency < 1 and script.flashlight.Value == true and act == true then
			print("yes")
			repeat
				print("truuu")
				if lp.PlayerGui.FlashLight.BatteryF.Bar:FindFirstChild("Bar4").BackgroundTransparency < 1 then
					if lp.PlayerGui.FlashLight.BatteryF.Bar:FindFirstChild("Bar"..bar).BackgroundTransparency < 1 then
						BT = lp.PlayerGui.FlashLight.BatteryF.Bar:FindFirstChild("Bar"..bar).BackgroundTransparency
						lp.PlayerGui.FlashLight.BatteryF.Bar:FindFirstChild("Bar"..bar).BackgroundTransparency += 0.01
					else
						bar +=1
					end
					
					

				end
				task.wait(timetogone)
			until lp.PlayerGui.FlashLight.BatteryF.Bar:FindFirstChild("Bar4").BackgroundTransparency > 1
		end
	end
end

https://i.gyazo.com/30a99dccb8aed46ff391edc8594dd514.gif

It seems that you’re never registering at the start of the function whether the battery is already going down. Simply make sure only one instance of this function is running at the same time by checking for a boolean that contains whether the function is already running

sorry I am not understanding can you expound

Just to confirm my theory, could you show when you call the drain battery function?

script.flashlight.Changed:Connect(function(val)
timetogone = tt

if val == true then
	act = true
	drainBattery(true)
	print("tr")
else
	act = false
	game.ReplicatedStorage.RemoteEvent:FireServer()
	drainBattery(false)
end

end)

Yeah, so basically, you need to check whether the function is already running, because you’re running the function everytime you click, but you never stop it from running when it’s clicked the second time (turned off), I would recommend you save whether there is already an instance of the function running before running it again, so you don’t have a duplicate

ok I did that before and it was still giving me problems

Could you show what you attempted to do then exactly, and what the issue was?