MouseButton1Click not working in this loop

So I have this code here

				if dialogue[DetectedNPC.Name].responses then
					for i,v in pairs(dialogue[DetectedNPC.Name].responses) do
						-- create a text button for each one
						local button = Instance.new("TextButton", game.Players.LocalPlayer.PlayerGui["Chat System"])
						button.Name = i
						button.Text = i
						button.Position = UDim2.new(0,200,0,0)
						button.BackgroundTransparency = 0
						button.TextSize = 30
					end
				end
				for i,v in pairs(game.Players.LocalPlayer.PlayerGui["Chat System"]:GetChildren()) do
					if v:IsA("TextButton") then
						v.MouseButton1Click:Connect(function()
							print("test")
						end)
					end
				end

And for some reason

						v.MouseButton1Click:Connect(function()
							print("test")
						end)

doesn’t seem to work at all and I’m not really sure why I’ve tried putting prints in the loop and they all work so the loop is working, but the button1click isn’t which is weird because I did this exact same thing in another script

for i = 1,#characters do
	local button = Instance.new("TextButton",rosterMenu)
	button.Name = characters[i]
	button.Text = characters[i]
end

for i,v in pairs(rosterMenu:GetChildren()) do
	if v:IsA("TextButton") then
		v.MouseButton1Click:Connect(function()
			if selected == nil then
				selected = v.Name
				displayCharacter(selected)
				print("Character Selected!")
			elseif v.Name == selected then
				print("Character Already Selected. Pick A New Character")
			elseif v.Name ~= selected then
				previousSelected = selected
				selected = v.Name
				displayCharacter(selected, previousSelected)
				print("Previous Selected:"..previousSelected.." New Selected: "..selected)
			end
		end)
	end
end

p.s: there are no errors

Hopefully someone is able to help and notice any issues because I don’t see what I did wrong lol.

1 Like

if dialogue[DetectedNPC.Name].responses then
for i,v in pairs(dialogue[DetectedNPC.Name].responses) do
– create a text button for each one
local button = Instance.new(“TextButton”, game.Players.LocalPlayer.PlayerGui[“Chat System”])
button.Name = i
button.Text = i
button.Position = UDim2.new(0,200,0,0)
button.BackgroundTransparency = 0
button.Size = UDim2.new(0,200,0,50)
button.TextSize = 30
end
end

for i,v in pairs(game.Players.LocalPlayer.PlayerGui[“Chat System”]:GetChildren()) do
if v:IsA(“TextButton”) then
v.MouseButton1Click:Connect(function()
print(“test”)
end)
end
end
–error was in size of button :confused:

1 Like

What? Also totally not making this message longer because of the limit lol

1 Like

oh well yeah that’s not the entire script the entire script is 150 lines lol dialogue variable is a module script and DetectedNPC is another variable

According to this post: Retriving the player that clicked,
it isn’t possible to use MouseButton1Clicked on the server, nor is it possible to detect it from a localscript on an other player’s UI.