I think this is more what you're looking for (I'm using Arcane intellect if you have a Mage or choose another spell to test that doesn't cost). I'm only counting down a couple of seconds from the start of the buff being applied so do with the timing as you will (x seconds before it runs out or whatever). Also printing the countdown so you can see it running.
Once a frame is hidden it's OnUpdate stops so I'm using a second frame for that and checking the event then using it to show the button if it finds the buff. This is where C_Timer is possibly a useful alternative but, this is just a basic starter example of how it might work.
I've started with the button hidden but that's up to you if you want it to be the primary source of the buff rather than just a popup reminder.
functions, variables etc. declared as local can't be used until after they're declared and only within the same code chunk.
Lua is case sensitive so UIparent is not the same as UIParent.
There are a bunch of other errors, if you don't have them installed I would suggest getting
BuggGrabber and
Bugsack so you can see when and mostly where/what your errors are.
Lua Code:
-- Setup the button
local button = CreateFrame("Button", "save-disk_MacroButton", UIParent, "SecureActionButtonTemplate, ActionButtonTemplate")
self:SetAlpha(0.3)
button:SetSize(24, 24)
--button:SetPoint("CENTER", mainframe, "CENTER", 0, 0)
button:SetPoint("CENTER")
button:SetAttribute("type1", "macro") -- left click causes macro
--button:SetAttribute("macrotext", "/cast Reflecting Prism")
button:SetAttribute("macrotext", "/cast Arcane Intellect")
button:SetText("Your Reflecting Prism will expire in 1 minute.")
button:SetNormalFontObject("GameFontNormalSmall")
button:SetNormalTexture("Interface/Icons/Inv_jewelcrafting_prism")
button:SetHighlightTexture("Interface/ICONS/Spell_nature_wispsplode")
button:SetPushedTexture("Interface/Icons/Spell_nature_wispsplode")
button:EnableMouse(true)
button:RegisterForClicks("LeftButtonUp")
button:SetScript("PostClick", function(self, arg1)
self:SetAlpha(0.3)
end)
-- Show button function (code could be rolled into the timer)
-- could be used to change the macro, icon, text depending on buff
-- could be used for other stuff.
local function popup(self)
self:SetAlpha(1)
PlaySound(1221)
FlashClientIcon()
end
-- Timer function
local function MyOnUpdate(self, elapsed)
self.TimeToCheck = self.TimeToCheck - elapsed
if self.TimeToCheck > 0 then
return -- We haven't counted down to zero yet so do nothing
end
self.TimeToCheck = 1 -- We've waited a second so reset the timer
for i=1,40 do
local name, icon, count, type, max_time, end_time, caster, buttonis_stealable = UnitAura("player", i);
if not name then break end -- no name means no more buffs to check
-- if name == "Reflecting Prism" then
if name == "Arcane Intellect" then
local TimeLeft = end_time - GetTime()-- check to see how much timer is left
print(name, TimeLeft)
if TimeLeft < 3595 then -- if we're in the zone
self:SetScript("OnUpdate", nil) -- cancel, the timer
if not button:IsShown() then
popup(button) -- show the button if required
end
return
end
end
end
end
-- Buff detection/timer starter frame
local Auraframe = CreateFrame("Frame")
Auraframe.TimeToCheck = 1
Auraframe:RegisterEvent("UNIT_AURA");
Auraframe:SetScript("OnEvent", function(self, event, ...)
-- if AuraUtil.FindAuraByName("Reflecting Prism", "player") then
if AuraUtil.FindAuraByName("Arcane Intellect", "player") then -- detect the buff has been applied
self:SetScript("OnUpdate", MyOnUpdate) -- start the timer
end
end)