Too much frames cloned in gui

So i got this problem when i made the gui prototype for my shop ui. My problem is that frame duplicates too much everytime. The gui works completly fine but when i check playergui in explorer this shows a tons of duplicated frames.
This video shows that theres tons of frames duplicated


The script of ui prototype

local buttons = script.Parent.Loadouts:GetChildren()
for i, button in pairs(buttons) do
	if button:IsA("TextButton") then
		button.MouseButton1Click:Connect(function()
			local frame = button:FindFirstChildOfClass("Frame"):Clone()
			frame.Parent = script.Parent.ViewingFrame
			script.Parent.ViewingFrame.FrameCurrent.Value = frame
			frame.Visible = true
			frame.Size = UDim2.new(1,0,1,0)
			frame.Position = UDim2.new(0,0,0,0)
			local fakeframe = script.Parent.ViewingFrame:FindFirstChildOfClass("Frame")
			if fakeframe and not script.Parent.ViewingFrame.FrameCurrent then
				fakeframe:Destroy()
				
			end
		end)
	end
end

If you guys spot any problem answer i would like to appreciate it

Is there a reason you are cloning the frames in? You could just parent three frames to ViewingFrame then add logic to change their visibility depending on which button you click.

1 Like

Can you just change the zindex of the ui making the one choosen be on the top?

1 Like

perhaps, you can do a function that checks if the frame is there already or not.
like this:

local buttons = script.Parent.Loadouts:GetChildren()
for i, button in pairs(buttons) do
	if button:IsA("TextButton") then
		button.MouseButton1Click:Connect(function()
			local frame = button:FindFirstChildOfClass("Frame"):Clone()
			if not script.Parent.ViewingFrame:FindFirstChild(frame.Name) then
				local fakeframe = script.Parent.ViewingFrame:FindFirstChildOfClass("Frame")
				if fakeframe --[[not script.Parent.ViewingFrame.FrameCurrent]] then
					fakeframe:Destroy()
				end
				frame.Parent = script.Parent.ViewingFrame
				script.Parent.ViewingFrame.FrameCurrent.Value = frame
				frame.Visible = true
				frame.Size = UDim2.new(1,0,1,0)
				frame.Position = UDim2.new(0,0,0,0)
			end
		end)
	end
end	

this code makes it so only one frame can be in the ‘ViewingFrame’, i’ve also fixed up your code a little bit.

pretty sure this is what you meant.

1 Like

that’s not what he meant, he meant that there can be too many frames duplicated by pressing the same button

1 Like

Oh… He can just check if the frame already exist or no

1 Like

thats what the code i sent has.

2 Likes

Maybe try not cloning a frame and formatting it accordingly. Try making the frames in advance and then program them to switch between. The idea is to use each and one frame with a unique identifier between, then hook functions to each and one of them to turn them visible or not.

Remember to assign a variable to check which frame is currently visible first so you can toggle it off when you switch.

1 Like

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