Can you use Tween.Completed for two different things?

I’m making a function to toggle a loading screen on and off with a transition. I set up a couple of tweens that i then tried to reuse for both the “On” and “Off” animation. The problem is that once a tween has finished the .Completed event runs for both cases is when things start to break. I’ve tried to look for something like this on the DevForum but with no results. so I’m writing a post here.

Here’s generally how the function looks like.


-- Tween1 and Tween2 are defined before the function
local function ToggleLoadingScreen(bool)
	Tween1:Play()

	Tween1.Completed:Connect(function()
		GUI.Visible = bool
		Tween2:Play()
	end)

end


ReplicatedStorage.MapLoading.Changed:Connect(function(bool)
	ToggleLoadingScreen(bool)
end)

I tried printing the bool value when Tween1 completed, which returned the correct value the first time the function ran but then started going back and forth between true and false with every next value change.

1 Like

You are creating more than 1 connection to the tween.Completed event, so this function is running more than once, a new connection is being made every time the remote event fires, which I believe would lead to the behaviour you’re experiencing.

You would have to disconnect your connection after you’re done with it

local function ToggleLoadingScreen(bool)
	Tween1:Play()

	local connection

    connection = Tween1.Completed:Connect(function()
		GUI.Visible = bool
		Tween2:Play()
        connection:Disconnect()
	end)

end


ReplicatedStorage.MapLoading.Changed:Connect(function(bool)
	ToggleLoadingScreen(bool)
end)

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