How can i make code wait until tween in other functions finishes(or wait until function return something)

  1. What do you want to achieve? Keep it simple and clear!
    So basically i have a code to tween any type of gui object with function in moduleScript. But i can’t make it wait until function finishes
  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tryed using repeat until function return true but it not really work

Code to call function:

for i=1,length do
		
		local skipped = TalkingLabel:GetAttribute("skipped")
		if skipped	== false then
			
			TalkingLabel.Text = StoryMessages:RetrieveMessage("Beginning...", "Messages", i)
			
			local check = ClientFunctions:AdjustTransparency(TalkingLabel, 0, 250)
			repeat task.wait(0.1); print(check) until check == true
			local reverseCheck = ClientFunctions:AdjustTransparency(TalkingLabel, 1, 250)
			repeat task.wait(0.1); print(check) until reverseCheck == true
		end
	end
	

Tween Function code:

function ClientFunctions:AdjustTransparency(UI, targetTransparency, Time, requireBackground)
	
	local currentTween
	local backgroundTween
	local UIStrokeTween
	local UIType
	
	local function ChangeTransparency(UIType)

		local TimeInSeconds = Time/1000
		local tweenInfo = TweenInfo.new(TimeInSeconds, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)

		local UITransparencyProperty = string.format("%sTransparency", UIType)

		if UIType == "Text" then
			currentTween = TweenService:Create(UI, tweenInfo, {TextTransparency = targetTransparency})
		elseif UIType == "Image" then
			currentTween = TweenService:Create(UI, tweenInfo, {ImageTransparency = targetTransparency})
		elseif UIType == "Background" then
			currentTween = TweenService:Create(UI, tweenInfo, {BackgroundTransparency = targetTransparency})
		end

		if requireBackground then
			backgroundTween = TweenService:Create(UI, tweenInfo, {BackgroundTransparency = targetTransparency})
			if UI:FindFirstChild("UIStroke") then
				UIStrokeTween = TweenService:Create(UI.UIStroke, tweenInfo, {Transparency = targetTransparency})
			end
		end
	end

	if UI:IsA("TextLabel") or UI:IsA("TextButton") then
		UIType = "Text"
	elseif UI:IsA("ImageLabel") or UI:IsA("ImageButton") then
		UIType = "Image"
	elseif UI:IsA("Frame") or UI:IsA("ScrollingFrame") then
		UIType = "Background"
	end
	
	ChangeTransparency(UIType)
	
	currentTween:Play()
	if requireBackground then
		backgroundTween:Play()
		
		if UI:FindFirstChild("UIStroke") then
			UIStrokeTween:Play()	
		end
	end
	currentTween.Completed:Connect(function()
		print(1)
		return true
	end)
end

1 Like

Instead of using a :Connect on the Completed event, you can instead use :Wait()

i.e.

currentTween.Completed:Wait()
task.wait(1)
return true

Oh, it works, thanks (30 letterssssssssssss)

You have to remember that return only stops the current task, return in this scenario is closing the .Completed event, not its parent.

Ouch. This code has a memory leak! Make sure to be disconnecting RBXScriptSignals like :Connect when something should be cleaned up. Here are better ways to write your .Completed event:

local connection
connection = currentTween.Completed:Connect(function()
    print(1)
    connection:Disconnect()
    return true
end)
currentTween.Completed:Once(function()
    print(1)
    return true
end)

The two above shouldn’t be used for this case specifically however because return doesn’t close the scope you’re trying to close, unless you wrapped it in a success call like a Promise. The best choice for you would be:

currentTween.Completed:Wait()
print(1)
return true

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