Why doesn't my loop end properly?

  1. What do you want to achieve?
    Hey I want to make a gui that turns semi-transparent after it haven’t been interacted with within a certain amount of time and turn opaque once the mouse hover over’s it again.

  2. What is the issue?
    The loop seems to increase the timer faster when you enter and leave the gui constantly and once the timer reaches its goal it seems to tween the transparency multiple times. Here is a gif of it happening, I added a print function to show it visually:

sm2B818j3p

  1. What solutions have you tried so far?
    I’ve tried to put the break function in different places but it still doesn’t seem to break, I’ve also added a debounce variable to try and see if it stops it from adding to timer multiple times but it also didn’t seem to work. At this point I’m basically out of idea’s and don’t know another way to fix so im asking for help

Here is part of my code for the script

local timer = 0
local breakCheck = false

mainFrame.MouseEnter:Connect(function()
	local tweenInfo2 = TweenInfo.new(0.6, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
	tweenService:Create(mainFrame, tweenInfo2, {BackgroundTransparency = 0}):Play()
	for _,item in pairs(mainFrame:GetDescendants()) do

		if item:IsA("Frame") and item.BackgroundTransparency ~= 1 then
			tweenService:Create(item, tweenInfo2, {BackgroundTransparency = 0}):Play()
		elseif item:IsA("TextLabel") then
			tweenService:Create(item, tweenInfo2, {TextTransparency = 0}):Play()
		elseif item:IsA("ImageButton") then
			tweenService:Create(item, tweenInfo2, {ImageTransparency = 0}):Play()
		end

	end
	breakCheck = true
	timer = 0
end)

mainFrame.MouseLeave:Connect(function()
	if breakCheck == true then
		breakCheck = false
	end
	while true do
		local deb = false
		if breakCheck then
			break
		else
			print("doing stuff")
			if timer == 8 then
				
				if deb then break end
				deb = true
				
				local tweenInfo2 = TweenInfo.new(0.6, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
				tweenService:Create(mainFrame, tweenInfo2, {BackgroundTransparency = 0.7}):Play()
				for _,item in pairs(mainFrame:GetDescendants()) do

					if item:IsA("Frame") and item.BackgroundTransparency ~= 1 then
						tweenService:Create(item, tweenInfo2, {BackgroundTransparency = 0.7}):Play()
					elseif item:IsA("TextLabel") then
						tweenService:Create(item, tweenInfo2, {TextTransparency = 0.7}):Play()
					elseif item:IsA("ImageButton") then
						tweenService:Create(item, tweenInfo2, {ImageTransparency = 0.7}):Play()
					end

				end
				
				wait()
				break
				
			else
				
				if deb then break end
				deb = true
				timer = timer + 1
				wait(1)
				deb = false

			end
			
		end

	end
	
end)

Please help me with any possible help because I’m out of ideas now. Thanks.

1 Like

You shouldn’t be using a loop here anyway. This use case is why TweenInfo.DelayTime exists.

3 Likes

Thanks ill give it a try soon.