How do I detect mousebutton1down with a clone TextButton?

I’ve been looking for SO long but I can’t find my answer to the question. I’m not sure if I’m doing something wrong or if I’m missing something. Here is my code I have so far.

(Developer Relations, please don’t flag me again.)

local Models = {}
local ModelNames = {}
local ModelSpawnSystem = game.StarterGui.ModelSpawnSystem
local CloneButton = game.StarterGui.ModelSpawnSystem.ScrollingFrame.CloneButton
for _, v in pairs(game.ReplicatedStorage.Models:GetChildren()) do
	table.insert(Models,v:GetExtentsSize())
	table.insert(ModelNames, v.Name)
end
for i , v in pairs(ModelNames) do
	local Clone = CloneButton:Clone()
	Clone.Visible = true
	Clone.Text = ModelNames[i]
	Clone.Parent = CloneButton.Parent
	Clone.Name = ModelNames[i]
end

for i, v in pairs(ModelSpawnSystem.ScrollingFrame:GetDescendants()) do
	if v:IsA("TextButton") then
		print("yes")
	end
end
print(Models)
print(ModelNames)

when you clone the button, you can create an event called MouseButton1Click on it.

either when cloning and parenting it or afterwards you can connect that event to a function.

1 Like

But wouldn’t the pair loop end already?

connections aren’t stuck to loops, they’re connected to the object itself. if in a loop you make a connection for MouseButton1Click, it’ll exist until you disconnect it or delete the button and Lua auto disconnects it

1 Like

Try using Activated instead of MouseButton1Click

1 Like

Tried it already but I might be doing it wrong.

You have tried this?

for i , v in pairs(ModelNames) do
	local Clone = CloneButton:Clone()
	Clone.Visible = true
	Clone.Text = ModelNames[i]
	Clone.Parent = CloneButton.Parent
	Clone.Name = ModelNames[i]

Clone.Activated:Connect(function()
-- code
end)
end
1 Like

Something like that. I tried it again just incase but it’s not printing a test string when I click one of the buttons.

1 Like

Nothing has worked so far but I’ll keep trying.

1 Like

Okay, I’ve figured out the answer by putting a localscript in the textbutton itsself. Now I’ve just got to figure out how to make it communicate to the server.

1 Like

For this use a RemoteEvent/Function for cross Client and Server or a BindableEvent/Function to send data between scripts

2 Likes

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