Tween still working even after script has been disabled

So I am trying to make when the flashlight value is off the tweeting of the color stops but even after I disable the script it still works

local LocalPlayer = game:GetService(“Players”).LocalPlayer;
local bar = 1
local lp = game.Players.LocalPlayer

local function drainBattery(bool)

if bool == true then
	while wait(0/1) do

		if lp.PlayerGui.FlashLight.BatteryF.Bar:FindFirstChild("Bar"..bar).BackgroundTransparency < 1 then

			print("yes")
			for i = 0, 1, 0.01 do
				local uiiElement = lp.PlayerGui.FlashLight.BatteryF.Bar:FindFirstChild("Bar"..bar)
				local startColor = Color3.new(1, 1, 1)
				local endColor = Color3.new(1, 0, 0)
				local fadeDuration = 10 

				local tweenInfo = TweenInfo.new(fadeDuration, Enum.EasingStyle.Linear)
				local tween = game:GetService("TweenService"):Create(uiiElement, tweenInfo, {BackgroundColor3 = endColor})
				tween:Play()
				script.Parent.flashlight.Changed:Connect(function(val)
					if val == false then
						local d = lp.PlayerGui.FlashLight.BatteryF.Bar:FindFirstChild("Bar"..bar):Clone()
						lp.PlayerGui.FlashLight.BatteryF.Bar:FindFirstChild("Bar"..bar):Destroy()
						tween:Destroy()
						d.Parent = lp.PlayerGui.FlashLight.BatteryF.Bar
					end
				end)
				LocalPlayer.PlayerGui.RemoteEvent.OnClientEvent:Connect(function()
					tween:Cancel()
					bar = 1
					for i, v in pairs(LocalPlayer.PlayerGui.FlashLight.BatteryF.Bar:GetChildren()) do
						v.BackgroundTransparency = 0
						v.BackgroundColor3 = Color3.fromRGB(255, 255, 255)

					end

				end)
				if lp.PlayerGui.FlashLight.BatteryF.Bar:FindFirstChild("Bar4").BackgroundTransparency < 1 then
					lp.PlayerGui.FlashLight.BatteryF.Bar:FindFirstChild("Bar"..bar).BackgroundTransparency += 0.01
					if lp.PlayerGui.FlashLight.BatteryF.Bar:FindFirstChild("Bar"..bar).BackgroundTransparency > 1 then
						if bar ~= 4 then
							bar +=1
						end

					end
					if bar == 4 and lp.PlayerGui.FlashLight.BatteryF.Bar:FindFirstChild("Bar"..bar).BackgroundTransparency > 1 then
						script.flashlight.Value = false
					end
				end


				wait(.1)
			end
		end
	end
end

end

script.Parent.flashlight.Changed:Connect(function(val)
if val == true then
drainBattery(true)
print(“tr”)
else
drainBattery(false)
end
end)

4 Likes

Since you are using a while loop you will need to stop the loop.
You can use the break keyword to end the loop. I’m not sure, but maybe you could check if the script is disabled and stop the loop.

If I’m wrong, then it is likely because I am not always the smartest mind and it is late for me, so my mind isn’t at the maximum alertness.

yes but break has to go before end so where would I put it?

Perhaps at the beginning of the loop you could put an if statement.
Try this:

if script.Enabled == false then
    break
end
1 Like

wait I am doing it in the for loop and not the while right?

1 Like

I believe the while loop is continuing to run after the script is disabled so you should try putting it at the beginning of the While loop then try the For loop.

Ok I tried both and still nothing

1 Like

Oop, it looks like you are checking a boolvalue. We are trying to check the Enabled property of the script, so you can replace script.flashlight.value with script.Enabled.

not I decided to change it so I can use if the flashlight is true or false

Hmm, then I’m not sure. Sorry :frowning: