Script not working on NPC menu

User Tag List

Results 1 to 4 of 4
  1. #1
    gravestalker's Avatar Active Member
    Reputation
    26
    Join Date
    Feb 2007
    Posts
    239
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Script not working on NPC

    I made a script using Lua and whenever I start combat with the boss he never starts the event. I was trying to make a boss and I put the script as a .lua file in my scripts folder.

    Here is the script
    Code:
    local NPCID = 799999
    function Elite_OnCombat(pUnit, event, player)
    Elite=pUnit
    pUnit:SendChatMessage(12, 0, "Puny Mortals, do you really think you can defeat me?")
    pUnit:RegisterEvent("Elite_Phase1", 1000, 1)
    end
    function Elite_OnLeaveCombat(pUnit, event, player)
     pUnit:RemoveEvents()
     pUnit:SendChatMessage(14, 0, "No one can defeat me!")
    end
    function Elite_OnDeath(pUnit, event, player)
     pUnit:RemoveEvents()
     pUnit:SendChatMessage(14, 0, "No! How can this be... I wil.. not... be... defeated!")
    end
    function Elite_Phase1(pUnit, event, player)
     if Elite:GetHealthPct() <= 75 then
     Elite:SendChatMessage(14, 0, "I will not stand for your distractions, I will finish you!")
     pUnit:RegisterEvent("Elite_Spell1", 120000, 1)
     pUnit:RegisterEvent("Elite_Phase2", 1000, 1)
    end
    function Elite_Phase2(pUnit, event, player)
     If Elite:GetHealthPct() <=30 then
     Elite:SendChatMessage(14, 0, "Minions, come to my aid!")
     pUnit:RegisterEvent("Elite_Summon", 1000, 20)
     pUnit:RegisterEvent("Elite_Phase3", 1000, 1)
    end
    function Elite_Phase3(pUnit, event, player)
     pUnit:SendChatMessage(14, 0, "Time to show you my real power!")
     pUnit:RegisterEvent("Elite_Spell2", 120000, 1)
    function Elite_Spell1(pUnit, Event)
    pUnit:CastSpell(20566)
    end
    function Elite_Spell2(pUnit, Event)
    pUnit:CastSpell(72143)
    end
     
    function Elite_Summon(pUnit, Event)
    local x = Unit:GetX();
    local y = Unit:GetY();
    local z = Unit:GetZ();
    local o = Unit:GetO();
    pUnit:SpawnCreature (799998, x, y, z, o, 14 ,600000);
    end
    RegisterUnitEvent(799999, 1, "Elite_OnCombat")
    RegisterUnitEvent(799999, 2, "Elite_OnLeaveCombat")
    RegisterUnitEvent(799999, 4, "Elite_OnDeath")
    Is there anything wrong with it or am I doing anything wrong that cause it not to work. I am using ArcEmu.

    Script not working on NPC
  2. #2
    stoneharry's Avatar Moderator Harry

    Authenticator enabled
    Reputation
    1613
    Join Date
    Sep 2007
    Posts
    4,554
    Thanks G/R
    150/146
    Trade Feedback
    0 (0%)
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You need to trace the code to find out what is going on. Think like the computer does.

    First things firsts it will find all the functions, hooks and variables not defined within a function.

    So as it occurs on the most part in a chronological order, the first thing it loads is this:
    Code:
    local NPCID = 799999

    Fine. Now it goes through each function.

    It gets to here:
    Code:
    function Elite_Phase3(pUnit, event, player)
         pUnit:SendChatMessage(14, 0, "Time to show you my real power!")
         pUnit:RegisterEvent("Elite_Spell2", 120000, 1)
        function Elite_Spell1(pUnit, Event)
        pUnit:CastSpell(20566)
    end
    Now it's confused. A second function starts before the first function ends. So it should spit you out an error in console.
    This needs to be fixed to look like this:
    Code:
    function Elite_Phase3(pUnit, event, player)
         pUnit:SendChatMessage(14, 0, "Time to show you my real power!")
         pUnit:RegisterEvent("Elite_Spell2", 120000, 1)
    end
    
    function Elite_Spell1(pUnit, Event)
        pUnit:CastSpell(20566)
    end
    It finally comes to the hooks:

    Code:
    RegisterUnitEvent(799999, 1, "Elite_OnCombat")
    RegisterUnitEvent(799999, 2, "Elite_OnLeaveCombat")
    RegisterUnitEvent(799999, 4, "Elite_OnDeath")
    These are fine but there was no point declaring NPCID if your not going to use it.

    Now lets assume the creature enters combat first:
    Code:
    function Elite_OnCombat(pUnit, event, player)
        Elite=pUnit
        pUnit:SendChatMessage(12, 0, "Puny Mortals, do you really think you can defeat me?")
        pUnit:RegisterEvent("Elite_Phase1", 1000, 1)
    end
    The Lua engine is already confused, what is Elite? You haven't declared it. Why are you setting this 'Elite' (which we don't know what it is) to the pUnit (which is a variable for the unit GUID).

    Code:
    function Elite_Phase1(pUnit, event, player)
         if Elite:GetHealthPct() <= 75 then
         Elite:SendChatMessage(14, 0, "I will not stand for your distractions, I will finish you!")
         pUnit:RegisterEvent("Elite_Spell1", 120000, 1)
         pUnit:RegisterEvent("Elite_Phase2", 1000, 1)
    end
    This is the next function that is triggered, and the Lua engine gets confused again. You start the function and end it - that is fine - but then you declare a if statement but don't end it. The Lua engine will treat this as a function in a function. The logic behind this is that the end you have here is ending the IF statement rather than the function. You need to add a second end like so:
    Code:
    function Elite_Phase1(pUnit, event, player)
         if Elite:GetHealthPct() <= 75 then
         Elite:SendChatMessage(14, 0, "I will not stand for your distractions, I will finish you!")
         pUnit:RegisterEvent("Elite_Spell1", 120000, 1)
         pUnit:RegisterEvent("Elite_Phase2", 1000, 1)
         end
    end
    You all also need to add pUnit:RemoveEvents() after the GetHealthPct bit because otherwise it will continue doing that function even though it's changed to another phase and hence cause loads of bugs.

    In phase two you are calling something that doesn't exist:
    Code:
         If Elite:GetHealthPct() <=30 then
         Elite:SendChatMessage(14, 0, "Minions, come to my aid!")
    What is Elite? You certainly haven't defined it. It will just error saying unit does not exist or a null reference.

    Finally the last mistake, that won't cause a error but is bad coding practice, is this:
    Code:
    function Elite_Summon(pUnit, Event)
        local x = Unit:GetX();
        local y = Unit:GetY();
        local z = Unit:GetZ();
        local o = Unit:GetO();
        pUnit:SpawnCreature (799998, x, y, z, o, 14 ,600000);
    end
    You don't need a semi-colon (;) after each line. This isn't C/Java etc where you have to end each process with a semi-colon, the Lua syntax is clever and doesn't require such methods.

    This should help you understand where your going and to fix up your script. Don't not read this post as it's a wall of text (or so it appears), if you want help you have to respond to the help given first. :)

  3. #3
    gravestalker's Avatar Active Member
    Reputation
    26
    Join Date
    Feb 2007
    Posts
    239
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, I was wondering exactly what I replace Elite with I read a guide ([Tutorial] How to Script a Phased Boss With LUA.) and it said to do the Elite=pUnit. I added the end functions one for the if statement and one for the function.

  4. #4
    stoneharry's Avatar Moderator Harry

    Authenticator enabled
    Reputation
    1613
    Join Date
    Sep 2007
    Posts
    4,554
    Thanks G/R
    150/146
    Trade Feedback
    0 (0%)
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    In that tutorial the person who posted obviously can not script Lua. It's wrong on so many levels.

    If you wanted to have it so you can call multiple npc's in multiple functions, you would do something like this:

    Code:
    local npcA = null
    local npcB = null
    
    function OnCombat_npcOne(pUnit, Event)
    npcA = pUnit
    end
    
    function OnCombat_npcTwo(pUnit, Event)
    npcB = pUnit
    end
    
    function AnotherFunctionLaterInTheScript(pUnit)
    npcA:SendChatMessage(12,0,"I am one unit!")
    npcB:SendChatMessage(12,0,"Whereas I am another!")
    end
    Alternatively you can do this:

    Code:
    function Stuff(pUnit)
          local NPC = pUnit:GetCreatureNearestCoords(x,y,z,npcid) -- Replace x,y,z,npcid with something suitable
          -- local NPC = pUnit:GetCreatureNearestCoords(pUnit:GetX(),pUnit:GetY(),pUnit:GetZ(),50) -- For example, where 50 is the npcid
          if NPC ~= null then 
              NPC:SendChatMessage(12,0,"You found me!")
          end
    end
    Last edited by stoneharry; 12-24-2010 at 03:02 PM.

Similar Threads

  1. NPC Lua script not working. (For unknown reason)
    By Joakim Karbing in forum WoW EMU Questions & Requests
    Replies: 4
    Last Post: 03-10-2016, 06:14 PM
  2. Script not working
    By LJN in forum World of Warcraft Emulator Servers
    Replies: 0
    Last Post: 07-16-2008, 02:47 PM
  3. [HELP] Lua boss script not working-solutions?
    By WinKIller0 in forum World of Warcraft Emulator Servers
    Replies: 15
    Last Post: 03-21-2008, 08:19 AM
  4. [Help] Lua Script Not Working
    By Muruk in forum World of Warcraft Emulator Servers
    Replies: 26
    Last Post: 03-16-2008, 02:13 PM
  5. Simple Lua Script , not working need help!
    By Arugos in forum World of Warcraft Emulator Servers
    Replies: 16
    Last Post: 12-30-2007, 02:06 PM
All times are GMT -5. The time now is 03:08 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search