Need help identifying TextButton that was clicked

I have 18 TextButtons and I want them to all run the same function when clicked but I
need to be able to identify what button was pressed.

I want to know if there is a way to identify what button was clicked if I do this.


function WeaponRetrieve(p2)
	local clickedButtonName = ????????
--	local PotentialDoubleClick = player.Backpack:FindFirstChild()
	
	
	
	
	
end

sc.TextButton1.MouseButton1Click:Connect(WeaponRetrieve)
sc.TextButton2.MouseButton1Click:Connect(WeaponRetrieve)
sc.TextButton3.MouseButton1Click:Connect(WeaponRetrieve)
sc.TextButton4.MouseButton1Click:Connect(WeaponRetrieve)
sc.TextButton5.MouseButton1Click:Connect(WeaponRetrieve)
sc.TextButton6.MouseButton1Click:Connect(WeaponRetrieve)
sc.TextButton7.MouseButton1Click:Connect(WeaponRetrieve)
sc.TextButton8.MouseButton1Click:Connect(WeaponRetrieve)
sc.TextButton9.MouseButton1Click:Connect(WeaponRetrieve)
sc.TextButton10.MouseButton1Click:Connect(WeaponRetrieve)

I would recommend doing this instead:

function WeaponRetrieve(button)
    local clickedButtonName = button.Name
end

for i,v in pairs(sc:GetChildren()) do
    if v:IsA("TextButton") then
        v.MouseButton1Click:Connect(function()
            WeaponRetrieve(v)
        end)
    end
end
1 Like