Azerothcore lua script doesnt work or load

113 Views Asked by At

i tried to write a script that removes poor (grey) items from the creature loot table globally, without touching the database. But it does not work and i can't figure out why because i am very new to lua programming and azerothcore in general.

i am using this azerothcore version: AzerothCore rev. 96a5224d927f+ 2023-04-22 07:25:31 +0700 (npcbots_3.3.5 branch) (Win64, Release, Static)

This is my script, it is stored in the lua_scripts folder with other lua scripts that seem to work:

-- Function to check if an item is grey (common)
function IsGreyItem(item)
    local itemQuality = item:GetQuality()
    return itemQuality == 0 -- 0 represents poor quality (common)
end

-- Hook into the loot drop event globally
function OnLoot(event, player, creature)
    local loot = creature:GetLoot()

    if not loot then
        return
    end

    for i = loot:GetSize(), 1, -1 do
        local item = loot:GetItem(i)

        if item and IsGreyItem(item) then
            -- Grey item found, remove it from the loot
            loot:RemoveItem(i)
        end
    end
end

-- Register the loot event handler globally
RegisterPlayerEvent(27) -- Register the loot event (ID 27) globally
AddEvent("OnCreatureLoot", OnLoot)

maybe someone has an idea why it doesn't work :)

i killed some low level mobs to check if they still drop grey items and they do.

2

There are 2 best solutions below

0
Honey55 On

The event is not registered properly. The syntax is

cancel = RegisterPlayerEvent( event, function )

So you need to replace your last 2 lines by

RegisterPlayerEvent( 27, OnLoot )

However, I suggest writing an SQL query that removes the grey items from the creature_loot_template and possibly other tables instead.

A good way to debug scripts, is to add print statements. These show text in the worldserver console. Firstly you see if the line is reached at all and you can also easily display values.

1
Chris Bunting On

You can also deselect the loot option for greys in the bot menu and the bots won't loot them.