Lua help please, Making button to trigger toy

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)

You’re probably wanting to use the spell that is cast when you click the toy, not the toy itself (different button attributes for toys, items, spells etc.).

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:RegisterForClicks("AnyDown", "AnyUp")
    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:SetAttribute("type", "toy")
    toyButton:SetAttribute("*toy1", 54452)
end

local eventFrame = CreateFrame("Frame")
eventFrame:RegisterEvent("PLAYER_LOGIN")
eventFrame:SetScript("OnEvent", function(self, event, arg1)
    if event == "PLAYER_LOGIN" then
        CreateToyButton()
    end
end)

I don’t have the portal but the Dalaran Hearstone (140192) is considered a toy

    toyButton:SetAttribute("type", "toy")
    toyButton:SetAttribute("*toy1", 140192)

Note: For future travelers, 54452 is a toy. The primary problem was having the actionButtonUseyKeyDown CVar set and only registering toyButton for AnyUp clicks.

Code above changed to reflect the desired result.

1 Like