My loop isn't stopping

My loop isn’t stopping, it continue do -0.1

local replicatedstorage = game:GetService("ReplicatedStorage")
local event = replicatedstorage.Event.Fade
local background = script.Parent.Frame.BackgroundTransparency
local db = false
local function unfade()
	while task.wait(0.1) do

		if background ~= 1 then
			script.Parent.Frame.BackgroundTransparency = script.Parent.Frame.BackgroundTransparency +0.1
		elseif background == 1 then
			script.Parent.Frame.Visible = false
			db = false
			break
		end

	end
end
local function fade()
	while task.wait(0.1) do

		if background ~= 0 then
			script.Parent.Frame.BackgroundTransparency = script.Parent.Frame.BackgroundTransparency -0.1
		elseif background == 0 then
			task.spawn(unfade)
			break
		end

	end
end

event.Event:Connect(function()
	if db == false then
		db = true

	script.Parent.Frame.BackgroundTransparency = 1
	script.Parent.Frame.Visible = true

	task.spawn(fade)
	end
end)

there are two loops

which loop???

1 Like

The fade function is the one that doesn’t stop.

It is continuing reaching negative numbers
image

  1. if this is a localscript, use .OnClientEvent instead of .Event
  2. just use tweenservice to reduce the transparency of the frame, then once tween is completed, task.spawn(unfade)
local tService = game:GetService("TweenService")

local tweenParameters = Tweeninfo.new(
1 -- time in seconds, this is how long the tween will take,
Enum.EasingDirection.Out,
Enum.EasingStyle.Linear
)

local fadeTween = tService:Create(InstanceName, tweenParameters, {BackgroundTransparency = 1})

fadeTween:Play() -- call when needed

fadeTween.Completed:Wait() -- will yield until the tween finishes
task.spawn(function)
1 Like

Cool, I didn’t know that with tweenservice you can do this. Thank you! I am testing it right now. I will let you know.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.