How to "restart" a loop/function (whatever solves the problem)

So basically I’m working on a plugin and one of its features is you being able to add your own textures that saves over multiple experiences with :getSetting()/:setSetting() and when you add a texture a new button is added into the scrolling frame but since I’m using a for i, v in frame:getChildren() it only appears but stuff like the title and adding the texture doesnt work.

TDLR:
I need help restarting this loop somehow without having to close the place and opening it up again.

Sorry for horrible code

for i, v in MainGui.Pbr:GetChildren() do
	if v:IsA("ImageButton") then
		v.MouseButton1Click:Connect(function()
			if v:GetAttribute("Custom") == false then
				for _, object in Selection:Get() do
					local importedTexture = script.Parent.Textures[v.Name]:Clone()
					importedTexture.Parent = Selection:Get()[1]
				end
			else
				for _, object in Selection:Get() do
					local importedTexture = Instance.new("SurfaceAppearance")
					importedTexture.Parent = Selection:Get()[1]
					importedTexture.Name = "CustomTexture"
					importedTexture.ColorMap = "rbxassetid://"..textures[v.Name].ColorMap
					importedTexture.MetalnessMap = "rbxassetid://"..textures[v.Name].MetallnessMap
					importedTexture.NormalMap = "rbxassetid://"..textures[v.Name].NormalMap
					importedTexture.RoughnessMap = "rbxassetid://"..textures[v.Name].RoughnessMap
				end
			end
		end)
		v.Title.Size = UDim2.new(0, 0, 0, 0)
		v.Title.TextTransparency = 1
		v.MouseEnter:Connect(function()
			currentSelected = v
			v.Title.Visible = true
			local Tween = TS:Create(v.Title, TweenInfo.new(.2, Enum.EasingStyle.Quart, Enum.EasingDirection.Out, 0, false ,0), {TextTransparency=0}):Play()
			v.Title:TweenSize(UDim2.new(0.746, 0,0.183, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quart, .2, false)
		end)
		v.MouseLeave:Connect(function()
			local Tween = TS:Create(v.Title, TweenInfo.new(.2, Enum.EasingStyle.Quart, Enum.EasingDirection.Out, 0, false ,0), {TextTransparency=1}):Play()
			v.Title:TweenSize(UDim2.new(0, 0, 0, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quart, .2, true, function()
				v.Title.Visible = false
			end)
		end)
		v.MouseButton2Click:Connect(function()
			inspect()
			currentSelected = v
		end)
	end
end
4 Likes

I would recommend containing the loop within a function, then just calling it whenever it’s needed (so at calling it at the start, and whenever the settings are changed).

You’ll need to make some other changes, such as checking if required connections have already been made, to avoid a memory leak. (if not connectionsMade then end)

2 Likes

I tried that the first time but that screwed up the whole script and kept calling the fuctions over and over again.
If you don’t mind I would really appreciate if you could send a code example just so I can understand roughly how it works. Thank you!

2 Likes

I reread your original post and came up with a simple method, where you have a function for setting up the buttons, then you simply call it repeatedly within the for loop, and also anytime ChildAdded is fired

function Setup(v)
	if v:IsA("ImageButton") then
		v.MouseButton1Click:Connect(function()
			if v:GetAttribute("Custom") == false then
				for _, object in Selection:Get() do
					local importedTexture = script.Parent.Textures[v.Name]:Clone()
					importedTexture.Parent = Selection:Get()[1]
				end
			else
				for _, object in Selection:Get() do
					local importedTexture = Instance.new("SurfaceAppearance")
					importedTexture.Parent = Selection:Get()[1]
					importedTexture.Name = "CustomTexture"
					importedTexture.ColorMap = "rbxassetid://"..textures[v.Name].ColorMap
					importedTexture.MetalnessMap = "rbxassetid://"..textures[v.Name].MetallnessMap
					importedTexture.NormalMap = "rbxassetid://"..textures[v.Name].NormalMap
					importedTexture.RoughnessMap = "rbxassetid://"..textures[v.Name].RoughnessMap
				end
			end
		end)
		v.Title.Size = UDim2.new(0, 0, 0, 0)
		v.Title.TextTransparency = 1
		v.MouseEnter:Connect(function()
			currentSelected = v
			v.Title.Visible = true
			local Tween = TS:Create(v.Title, TweenInfo.new(.2, Enum.EasingStyle.Quart, Enum.EasingDirection.Out, 0, false ,0), {TextTransparency=0}):Play()
			v.Title:TweenSize(UDim2.new(0.746, 0,0.183, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quart, .2, false)
		end)
		v.MouseLeave:Connect(function()
			local Tween = TS:Create(v.Title, TweenInfo.new(.2, Enum.EasingStyle.Quart, Enum.EasingDirection.Out, 0, false ,0), {TextTransparency=1}):Play()
			v.Title:TweenSize(UDim2.new(0, 0, 0, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quart, .2, true, function()
				v.Title.Visible = false
			end)
		end)
		v.MouseButton2Click:Connect(function()
			inspect()
			currentSelected = v
		end)
	end
end

for i, v in MainGui.Pbr:GetChildren() do
	Setup(v)
end
MainGui.Pbr.ChildAdded:Connect(Setup)
3 Likes

DUDE THANK YOU SOOOOOOO MUCH!!! I’ve been trying to fix this for so long!!!

1 Like

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