I need to break a loop with a function using pairs

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want the pair loop to break using a mousebutton1click function but the break says its not in a loop

  1. What is the issue? Include screenshots / videos if possible!

it errors by saying its not in a loop

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

i searched all over dev fourm but its talking about while and repeat loops but not about pair loops.

local tableofnames = {"UpKeybind","RightKeybind","LeftKeybind","DownKeybind"}
for _,button in pairs(script.Parent:GetChildren()) do
	if button:IsA("TextButton") and table.find(tableofnames,button.Name) then
		button.MouseButton1Click:Connect(function()
			if script.Parent.ScrollingFrame.Visible == true then return end
			script.Parent.ScrollingFrame.Visible = true
			for _,key in pairs(Enum.KeyCode:GetEnumItems()) do -- i need to break this loop
				if string.find(key.Name,"World") or key == Enum.KeyCode.Unknown then else
					local clone = script.Parent.ScrollingFrame.TextButton:Clone()
					clone.Name = key.Name
					clone.Text = key.Name
					clone.Parent = script.Parent.ScrollingFrame
					clone.Visible = true
					clone.MouseButton1Click:Connect(function()
						button.Text = key.Name
						break -- but it says this isnt in a loop
					end)
				end
			end
		end)
	end
end

Never mind I found a fix, I just clone the scrolling frame and whenever a button is pressed in the scrolling frame it destroys itself. Here’s the script if you need it.

local tableofnames = {"UpKeybind","RightKeybind","LeftKeybind","DownKeybind"}
for _,button in pairs(script.Parent:GetChildren()) do
	if button:IsA("TextButton") and table.find(tableofnames,button.Name) then
		button.MouseButton1Click:Connect(function()
			local a = game.ReplicatedStorage.ScrollingFrame:Clone()
			a.Parent = script.Parent
			a.Visible = true
			for _,b in pairs(a:GetChildren()) do
				if b:IsA("TextButton") then
					b.MouseButton1Click:Connect(function()
						button.Text = b.Name
						a:Destroy()
					end)
				end
			end
		end)
	end
end
1 Like