Completed Events for Tweens Running Multiple Times

Hello!

I have a script for the game I’m codeveloping, and when you enter a new zone in the game, a GUI pops up and some other things happen. The first time you enter a zone, everything works perfectly. However, every zone you enter after that, the Tween Completed events are running multiple times (for each previous zone entered) for some reason I can’t figure out.

The problem is illustrated here, in the console, where the output should only print that the zone GUI is completed for the area you enter. I have no idea why or how the “Completed Zone” Text is being printed multiple times, with previous inputs that are no longer inputted. If I enter 3 zones, It happens 3 times and so on.

image

Code
local function PlayZoneAnimation(TextString, ZoneSongName)

	if not playingAnim then
		print("Entering " .. TextString)
		playingAnim = true 
		local NewSong = coroutine.wrap(function()
			PlayNewSong(ZoneSongName)
		end)
				
		NewSong()
		print(TextString)	
		tweenHeight:Play()
		
		tweenHeight.Completed:Connect(function()
			--Runs with previous TextStrings
			print("Completed " .. TextString)
			tweenWidth:Play()
		end)
		
		tweenWidth.Completed:Connect(function()
			task.wait(.2)
			local CurrentString = ''
				for i = 1, #TextString do
					local nextLetter = string.char(TextString:byte(i))
					CurrentString = CurrentString .. nextLetter
					TextLabel.Text = CurrentString
					TypeSound(Frame)
					task.wait(.07)
			end
			tweenClose:Play()	
		end)
		
		tweenClose.Completed:Connect(function()
			TextLabel.Text = ''
			Frame.Size = UDim2.new(0.1, 0, 0, 0)
			playingAnim = false
			debounce = false
		end)
		
	end
end

I couldn’t find any solutions on the Developer Hub, and I’ve been stuck on attempting to fix this myself for about an hour. Any help is appreciated!

2 Likes

Try replacing all the :Connect 's with :Once

You’re making new connections every time you enter a new zone which don’t get removed, so using :Once makes sure it only happens a single time

2 Likes

Thank you! I never even heard about Once() method before, and I’ve spent hours upon hours reading documentation and watching informational tutorials. Now I understand RBXScriptSignals and connections better.

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