Help needed with If statement in addon

I am creating my own addon with some UI configurations etc and most of it seem to work fine.

But I am having an issue with the following code where I am trying to hide the stancebar (stealth button) when I login on my rogue:

local f = CreateFrame("frame")
f:RegisterEvent("PLAYER_LOGIN")
f:SetScript("OnEvent", function()
	if (UnitClass("Player") == "Rogue") then
		StanceBarFrame:Hide()
	end
end)

After logging in the stance bar is still visible, but running below while ingame hides it successfully:
/run if UnitClass("Player") == "Rogue" then StanceBarFrame:Hide() end

Also using the same code for detecting login works for sending a basic chat message to test etc. I can’t see what I am doing wrong.

Update:
I now tried this

local f = CreateFrame("frame")
f:RegisterEvent("PLAYER_LOGIN")
f:SetScript("OnEvent", function()
	if (_G.UnitClass("Player") == "Rogue") then
		print("Class is Rogue")
		StanceBarFrame:Hide()
	else
		print("Class is not Rogue")
	end
end)

And it does print “Class is Rogue” in the chatbox when i login to my rogue, so my IF statement does work. It just wont hide the StanceBarFrame through it.
Just running /run StanceBarFrame:Hide() ingame works fine. It’s almost as if Blizzards ui loads it after my code already has run

Maybe you could try hooking a script to the Show method of StanceBarFrame.

local f = CreateFrame("frame")
f:RegisterEvent("PLAYER_LOGIN")
f:SetScript("OnEvent", function()
    StanceBarFrame:HookScript("OnShow" function(self)
        if UnitClass("Player") == "Rogue" then
            self:Hide()
        end
    end)
end)

Thanks, it looked promising but unfortunately it did not work either.
So far i’ve concluded that my IF statement works atleast as I’ve shown in my updated post.
Just need to figure out why it doesn’t want to hide on load.

Update:
I found the solution, calling StanceBarFrame:Hide() from my addon would not work in any way. After searching some more i found that the following worked instead:
RegisterStateDriver(StanceBarFrame, "visibility", "hide")
I just replaced StanceBarFrame:Hide() in my code with above and it worked fine! :slight_smile:

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