luaazerothcoreeluna-lua-engine

I want lua function to run once


I'm quite new to lua scripting.. now i'm trying to code in game boss

local function SlitherEvents(event, creature, attacker, damage)
    if(creature:GetHealthPct() <= 60) then
        creature:SendUnitYell("Will punish you all",0)
        creature:RegisterEvent(AirBurst, 1000, 0) -- 1 seconds
        return
    end
end

this should make the boss talk when his health = 60% or less but it should run one time, when I run the code the boss keep saying and attacking all the time. How can I make it run once?


Solution

  • Use a boolean created outside the scope of the function callback:

    local has_talked = false
    local function SlitherEvents(event, creature, attacker, damage)
      if creature:GetHealthPct() <= 60 and not has_talked then
        has_talked = true
        creature:SendUnitYell("Will punish you all",0)
        creature:RegisterEvent(AirBurst, 1000, 1) -- 1 seconds
        return
      end
    end
    

    EDIT

    If you are actually using the Eluna Engine's RegisterEvent call, then set the number of repeats to 1 and not 0. This will resolve the issue you had.