I am trying to create a simple addon that creates a toyButton frame, and sets the button to a hearth toy. “C_ToyBox.UseToy(54452)” throws a Lua Error “UseToy (a nil value)”, indicating that the UseToy function is not available or does not exist. If I remove “C_ToyBox.” and just try “UseToy”, it gives me the popup that the action was blocked and only available to Blizzard UI. I am creating the toyButton with “SecureActionButtonTemplate”, which I think should allow the secure function “UseToy” to work while out of combat. Can anyone with Lua coding knowledge or experience help me? Thanks in advance.
Here is the code:
local toyButton
local function CreateToyButton()
local _, toyName, toyTexture = C_ToyBox.GetToyInfo(54452) -- Example: Ethereal Portal
toyButton = CreateFrame("Button", "HearthToyButton", UIParent, "SecureActionButtonTemplate")
toyButton:SetSize(41, 41)
toyButton:SetNormalTexture(toyTexture or "Interface\\Icons\\INV_Misc_QuestionMark")
toyButton:SetPoint("CENTER", UIParent, "CENTER")
toyButton:SetScript("OnEnter", function(self)
GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
GameTooltip:SetToyByItemID(54452) -- Update with the correct toy item ID
GameTooltip:Show()
end)
toyButton:SetScript("OnLeave", function(self)
GameTooltip:Hide()
end)
toyButton:RegisterForClicks("AnyUp")
toyButton:SetScript("OnClick", function(self, button)
if button == "LeftButton" then
if InCombatLockdown() then
print("Cannot use toy in combat")
else
C_ToyBox.UseToy(54452) -- Update with the correct toy item ID
end
end
end)
end
local eventFrame = CreateFrame("Frame")
eventFrame:RegisterEvent("PLAYER_LOGIN")
eventFrame:SetScript("OnEvent", function(self, event, arg1)
if event == "PLAYER_LOGIN" then
CreateToyButton()
end
end)