Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is it possible to use a MouseButton1Click function in a loop that creates text guis?

I don't know is the question properly reflects what I'm trying to do, but I'm not sure how to word it. I'm making several textbuttons and I want to be able to tell whenever one of them is pressed, but they all have the same name and I don't want to have to adjust my script to accommodate different names and I don't want to have to write functions for each name. My code:

for i,v in pairs(spweps) do
			box = primary:Clone()
			box.Parent = loadoutbg
			box.Visible = true
			box.Position = primary.Position + UDim2.new(0.0,0,0.11*i,0) - UDim2.new(0.0,0,0.11,0)
			box.Text = v
			box.Name = "gunselect"
			v.MouseButton1Click:connect(function() --line 80 in case you couldn't guess
				if game.Lighting:FindFirstChild(v) then
					local gun = game.Lighting[v]
					player.Primary.Value = gun
				else
					print("Not in lighting")	
				end
			end)	
		end

Output: Players.Player1.PlayerGui.SpawnGui.Menu:80: attempt to index field 'MouseButton1Click' (a nil value) A piggyback question I have on this one is is it possible to have multiple guis that when clicked fire the same function? something like this:

gui1.mb1d or gui2.mb1d or gui3.mb1d:connect(function()
	--Stuff
end)

or

local numbers = {"1", "2", "3"}
for i,v in pairs(numbers) do
	gui..v.mb1d:connect(function()
		--Stuff
	end
end

1 answer

✓ Accepted answer
FuncTabe = { }
Guis = GUI_LOCATIONG:GetChildren()
for i = 1, #Guis do
	FuncTable[#FuncTable + 1] = Guis[i].MouseButton1Down:connect(function()

		
		GuiLink = Guis[i]
		print(GuiLink) --This lets you know which was pressed.
		--What happens with the click happenes here.
		

	end)
end

This discussion is preserved as it was. New answers, comments, and votes are closed.