Where you're setting the OnClick for each button, you're using a fixed string to print.
print(buttonName)
Where is buttonName defined? If it's global and being overwritten before you get to click the that's what it's value will be. If it's local inside a chunk the OnClick handlers can't "see" then it will be nil or???
Lua Code:
local index = 0
while true do
index = index + 1
local buttonName = "PetJournalListScrollFrameButton" .. index
print(buttonName) -- debug --
local button = _G[buttonName]
if not button then break end
button:HookScript("OnClick", function(self)
print(self:GetName()) -- debug --
end)
end
Seems to work.