Multiple Frames getting Cloned After Exiting the main frame

I made this map selector Gui that opens up when u enter the elevator. Then it loops through the available maps on the Main Gui and it will look thru replicated storage thru a folder containing the map name and the number of levels. A level template in replicated storage will be cloned and be modified accoring to the configuration values of the level.

It works Fine on the first entry but when I exit the elevator and re-enter this happens…:

this is the map window:
image
and the level templates will get cloned into this frame:
image
The Episodes/Levels folder with boolValues to determine if they are the default map or default level (The default map and its corresponding default level with be animated and pre selected the moment you enter the elevator)

Here is the For loop…

for i, map in pairs(MapWindow:GetChildren())do


			if map:IsA("ImageButton") then
				local DefaultMap
				local Button = map
				
				Button.ImageTransparency = 0.67
				Button.Highlight.Transparency = 1
				Button.Size = UDim2.new(0.65,0,0.65,0)

				
				
				for i, Values in pairs(ReplicatedStorage.Episodes[Button.Name]:GetChildren())do
					if Values:IsA("BoolValue") then
						DefaultMap = Values.Parent.Name
					end
				end
				
				if Button.Name == DefaultMap and not SelectedButton then
					for i, level in pairs(gui.LevelSelector.Episodes.EpisodeWindow:GetChildren()) do
						if level:IsA("ImageButton") then
							level:Destroy()
						end
					end
					
					SelectedButton = Button
					Button.ImageTransparency = 0.2
					Button.Highlight.Transparency = 0

					
					StageChange:FireServer("Map", DefaultMap)

					coroutine.wrap(function()
						TweenService:Create(Button,TweenInfo.new(0.18,Enum.EasingStyle.Back),{Size = UDim2.new(0.85,0,0.85,0)}):Play()
						task.wait(0.05)
						TweenService:Create(Button,TweenInfo.new(0.12,Enum.EasingStyle.Back),{Size = UDim2.new(0.63,0,0.63,0)}):Play()
						task.wait(0.05)
						TweenService:Create(Button,TweenInfo.new(0.2,Enum.EasingStyle.Back),{Size = UDim2.new(0.65,0,0.65,0)}):Play()
					end)()
					
					
					for i, Levels in pairs(ReplicatedStorage.Episodes[DefaultMap]:GetChildren())do
						if Levels:IsA("Configuration") then
							local DefaultLevel


							local template = ReplicatedStorage.Episodes.EpisodeTemplate
							local newTemplate = template:Clone()

							for i, Values in pairs(Levels:GetChildren()) do
								if Values:IsA("BoolValue") then
									DefaultLevel = Values.Parent.LevelName.Value
								end
							end
							print(DefaultLevel)
							newTemplate.LevelName.Text = Levels.LevelName.Value
							newTemplate.Name = Levels.LevelName.Value
							newTemplate.Number.Text = Levels.Level.Value
							newTemplate.LayoutOrder = Levels.LayoutOrder.Value
							newTemplate.Parent = gui.LevelSelector.Episodes.EpisodeWindow

							if newTemplate.Name == DefaultLevel then
								StageChange:FireServer("Level", DefaultLevel)
								newTemplate.UIStroke.Transparency = 0
								newTemplate.Frame.BackgroundTransparency = 0.9
								SelectedLevel = newTemplate
								coroutine.wrap(function()
									TweenService:Create(newTemplate,TweenInfo.new(0.3,Enum.EasingStyle.Back),{Size = UDim2.new(0,300,0,70)}):Play()
									task.wait(0.05)
									TweenService:Create(newTemplate,TweenInfo.new(0.12,Enum.EasingStyle.Back),{Size = UDim2.new(0,150,0,45)}):Play()
									task.wait(0.05)
									TweenService:Create(newTemplate,TweenInfo.new(0.2,Enum.EasingStyle.Back),{Size = UDim2.new(0,200,0,50)}):Play()
								end)()

							end


							newTemplate.Activated:Connect(function()
								if SelectedLevel == newTemplate then return end
								if SelectedLevel then
									SelectedLevel.UIStroke.Transparency = 1
									SelectedLevel.Frame.BackgroundTransparency = 0.15
									TweenService:Create(newTemplate,TweenInfo.new(0.18,Enum.EasingStyle.Back),{Size = UDim2.new(0.768,0,0.05,0)}):Play()
									task.wait()

								end
								SelectedLevel = nil
								task.wait()
								SelectedLevel = newTemplate
								SelectedLevel.UIStroke.Transparency = 0
								SelectedLevel.Frame.BackgroundTransparency = 0.9
								StageChange:FireServer("Level", newTemplate.Name)
								coroutine.wrap(function()
									TweenService:Create(newTemplate,TweenInfo.new(0.3,Enum.EasingStyle.Back),{Size = UDim2.new(0,300,0,70)}):Play()
									task.wait(0.05)
									TweenService:Create(newTemplate,TweenInfo.new(0.12,Enum.EasingStyle.Back),{Size = UDim2.new(0,150,0,45)}):Play()
									task.wait(0.05)
									TweenService:Create(newTemplate,TweenInfo.new(0.2,Enum.EasingStyle.Back),{Size = UDim2.new(0,200,0,50)}):Play()
								end)()

							end)
						end



					end
				
				
				end

				Button.Activated:Connect(function()
					

					if SelectedButton == Button then return end	
					
					

					if SelectedButton then
						SelectedButton.ImageTransparency = 0.68
						SelectedButton.Highlight.Transparency = 1
						TweenService:Create(SelectedButton,TweenInfo.new(0.2),{Size = UDim2.new(0.65,0,0.65,0)}):Play()
						for i, level in pairs(gui.LevelSelector.Episodes.EpisodeWindow:GetChildren()) do
							if level:IsA("ImageButton") then
								level:Destroy()
							end
						end
						SelectedButton = nil


					end


					Button.ImageTransparency = 0.2
					Button.Highlight.Transparency = 0
					
					--ChangeMap
					--player.MapsInfo.ChosenMap.Value = Button.Name
					StageChange:FireServer("Map", Button.Name)

					coroutine.wrap(function()
						TweenService:Create(MapWindow["The Park"],TweenInfo.new(0.18,Enum.EasingStyle.Back),{Size = UDim2.new(0.85,0,0.85,0)}):Play()
						task.wait(0.05)
						TweenService:Create(MapWindow["The Park"],TweenInfo.new(0.12,Enum.EasingStyle.Back),{Size = UDim2.new(0.63,0,0.63,0)}):Play()
						task.wait(0.05)
						TweenService:Create(MapWindow["The Park"],TweenInfo.new(0.2,Enum.EasingStyle.Back),{Size = UDim2.new(0.65,0,0.65,0)}):Play()
					end)()

					SelectedButton = nil
					task.wait()

					SelectedButton = Button
					print(SelectedButton)

					for i, Levels in pairs(ReplicatedStorage.Episodes[SelectedButton.Name]:GetChildren())do
						if Levels:IsA("Configuration") then
							local DefaultLevel


							local template = ReplicatedStorage.Episodes.EpisodeTemplate
							local newTemplate = template:Clone()

							for i, Values in pairs(Levels:GetChildren()) do
								if Values:IsA("BoolValue") then
									DefaultLevel = Values.Parent.LevelName.Value
								end
							end
							print(DefaultLevel)
							newTemplate.LevelName.Text = Levels.LevelName.Value
							newTemplate.Name = Levels.LevelName.Value
							newTemplate.Number.Text = Levels.Level.Value
							newTemplate.LayoutOrder = Levels.LayoutOrder.Value
							newTemplate.Parent = gui.LevelSelector.Episodes.EpisodeWindow

							if newTemplate.Name == DefaultLevel then
								StageChange:FireServer("Level", DefaultLevel)
								newTemplate.UIStroke.Transparency = 0
								newTemplate.Frame.BackgroundTransparency = 0.9
								SelectedLevel = newTemplate
								coroutine.wrap(function()
									TweenService:Create(newTemplate,TweenInfo.new(0.3,Enum.EasingStyle.Back),{Size = UDim2.new(0,300,0,70)}):Play()
									task.wait(0.05)
									TweenService:Create(newTemplate,TweenInfo.new(0.12,Enum.EasingStyle.Back),{Size = UDim2.new(0,150,0,45)}):Play()
									task.wait(0.05)
									TweenService:Create(newTemplate,TweenInfo.new(0.2,Enum.EasingStyle.Back),{Size = UDim2.new(0,200,0,50)}):Play()
								end)()

							end


							newTemplate.Activated:Connect(function()
								if SelectedLevel == newTemplate then return end
								if SelectedLevel then
									SelectedLevel.UIStroke.Transparency = 1
									SelectedLevel.Frame.BackgroundTransparency = 0.15
									TweenService:Create(newTemplate,TweenInfo.new(0.18,Enum.EasingStyle.Back),{Size = UDim2.new(0.768,0,0.05,0)}):Play()
									task.wait()
									

								end
								
								task.wait()
								SelectedLevel = newTemplate
								SelectedLevel.UIStroke.Transparency = 0
								SelectedLevel.Frame.BackgroundTransparency = 0.9
								StageChange:FireServer("Level", newTemplate.Name)
								coroutine.wrap(function()
									TweenService:Create(newTemplate,TweenInfo.new(0.3,Enum.EasingStyle.Back),{Size = UDim2.new(0,300,0,70)}):Play()
									task.wait(0.05)
									TweenService:Create(newTemplate,TweenInfo.new(0.12,Enum.EasingStyle.Back),{Size = UDim2.new(0,150,0,45)}):Play()
									task.wait(0.05)
									TweenService:Create(newTemplate,TweenInfo.new(0.2,Enum.EasingStyle.Back),{Size = UDim2.new(0,200,0,50)}):Play()
								end)()

							end)
						end
						
						

					end



				end)

			end

		end
		
5 Likes

Hey!
Can you make sure the ImageLabels are actually getting destroyed? To me it seems like that might be the issue.
You can check this by adding a simple print above (or below) the line where it’s being destroyed

2 Likes

image
yess they are getting destroyed…
I also checked the player gui when running the destroy loop and they seem to dissapear

2 Likes

Here is the Exit Button Script btw

exitbutton.Activated:Connect(function()
			gui.LevelSelector.Visible = false
			stopCountdown = true
			InsideElevator = false
			CountdownRunning = false
			SelectedButton = nil
			camera.CameraType = Enum.CameraType.Custom
			camera.FieldOfView = 70
			camera.CameraSubject = player
			

			ElevatorEvent:FireServer("RemoveAll")
			for i, level in pairs(gui.LevelSelector.Episodes.EpisodeWindow:GetChildren()) do
				if level:IsA("ImageButton") then
					level:Destroy()
					print("DestroyedFrames")
				end
			end
		end)
1 Like

Hm…
How about printing: ReplicatedStorage.Episodes[DefaultMap]:GetChildren()?

1 Like

1 Like

No no, not there. Sorry for being too unclear.
You have this spot in your code where you loop through the GetChildren() table I specified to clone and insert the ImageLabels, I just wanted you to print that table that’s being looped through.

1 Like



I just printed out the levels. The problem is that it adds duplicate Levels whenever I exit. Im trying to figure out why that happends

1 Like

Maybe it isn’t about the exiting itself, but rather it may be about the the fact that it’s getting the levels multiple times? I’ll have to read the code in more detail to be able to tell

1 Like

Heyy mann What have you come up withh? sorry for the late response

1 Like