Checking for mousebutton1click on a button made with instance.new, but only if it has been created

I’m making a visual novel engine on roblox and I wanna add a system where someone can add buttons to give the player a choice in the dialogue, each with different outcomes.
but the issue is, I want to make it easy to understand so anyone no matter their coding experience can use my engine, and they only have to use an if statement just like this one to check player choices

if choice == 1 then
	-- some dialogue
elseif choice == 2 then
	-- other dialogue
end

adding a mousebutton1click function to the condition that created the instance didn’t work, and if I do it outside of the conditions it will cause an error when it gets to a button that doesn’t exist because there weren’t enough choices to fill up all slots

this is my code, sorry for the spaghetti code I’m going to optimize it once it is functional (keep in mind that this is in a function and there is more code before this in the function)

if not choice then
		if not configs.skip then
			if not configs.auto then
				repeat game:GetService("RunService").RenderStepped:Wait() until KeyVerify()
			else
				wait(1)
			end
		else
			game:GetService("RunService").RenderStepped:Wait()
		end
	else
		print("choice")
		if one == nil and two == nil and three == nil and four == nil and five == nil and six == nil then
			warn("No buttons found. Falling back to normal response.")
			if not configs.skip then
				if not configs.auto then
					repeat game:GetService("RunService").RenderStepped:Wait() until KeyVerify()
				else
					wait(1)
				end
			else
				game:GetService("RunService").RenderStepped:Wait()
			end
		end
		if one ~= nil then
			local button = Instance.new("TextButton", main.optionframe)
			local uicorner = Instance.new("UICorner", button)
			button.BackgroundColor3 = Color3.fromRGB(47,47,47)
			button.TextColor3 = Color3.new(1,1,1)
			button.TextSize = configs.textsize
			button.TextWrapped = true
			
			button.Text = one
			button.Name = "button1"
		end
		if two ~= nil then
			local button = Instance.new("TextButton", main.optionframe)
			local uicorner = Instance.new("UICorner", button)
			button.BackgroundColor3 = Color3.fromRGB(47,47,47)
			button.TextColor3 = Color3.new(1,1,1)
			button.TextSize = configs.textsize
			button.TextWrapped = true

			button.Text = two
			button.Name = "button2"
		end
		if three ~= nil then
			local button = Instance.new("TextButton", main.optionframe)
			local uicorner = Instance.new("UICorner", button)
			button.BackgroundColor3 = Color3.fromRGB(47,47,47)
			button.TextColor3 = Color3.new(1,1,1)
			button.TextSize = configs.textsize
			button.TextWrapped = true

			button.Text = three
			button.Name = "button3"
		end
		if four ~= nil then
			local button = Instance.new("TextButton", main.optionframe)
			local uicorner = Instance.new("UICorner", button)
			button.BackgroundColor3 = Color3.fromRGB(47,47,47)
			button.TextColor3 = Color3.new(1,1,1)
			button.TextSize = configs.textsize
			button.TextWrapped = true

			button.Text = four
			button.Name = "button4"
		end
		if five ~= nil then
			local button = Instance.new("TextButton", main.optionframe)
			local uicorner = Instance.new("UICorner", button)
			button.BackgroundColor3 = Color3.fromRGB(47,47,47)
			button.TextColor3 = Color3.new(1,1,1)
			button.TextSize = configs.textsize
			button.TextWrapped = true

			button.Text = five
			button.Name = "button5"
		end
		if six ~= nil then
			local button = Instance.new("TextButton", main.optionframe)
			local uicorner = Instance.new("UICorner", button)
			button.BackgroundColor3 = Color3.fromRGB(47,47,47)
			button.TextColor3 = Color3.new(1,1,1)
			button.TextSize = configs.textsize
			button.TextWrapped = true

			button.Text = six
			button.Name = "button6"
		end
	end

bump bc i still haven’t figured it out

You could use an array to store the ‘answers’ or ‘dialogue’ and when hooking up the buttons just see how many are within the array #answers, #dialogue.

local answers = {
    "Answer 1",
    "Answer 2", -- etc
}

Should also prevent the need for doing this:
if one == nil and two == nil and three == nil and four == nil and five == nil and six == nil then

kind of odd to make an engine ON an engine, but ok. also if people cant understand that code they shouldnt be making games, so just add a disclaimer saying “you will require intermediate programming knowledge if you wish to modify the code”