• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

TFS 1.X+ Loot Bonus Item based on monster loot.

OTcreator

Well-Known Member
Joined
Feb 14, 2022
Messages
503
Solutions
1
Reaction score
57
Hello.

I am trying to create a script that will bass on monster loot.

Namely, after killing a monster and drawing match random(500) < 10 then the player will receive an item that has one of the smallest chances specified in the monster loot (that is, for example, will receive something from the loot with a chance between 500-10)

So far I have something like this and I can't manage it.

Can anyone help ?

LUA:
function onKill(cid, target)
    -- Sprawdzamy, czy potwór został zabity
    if isMonster(target) then
        -- Losujemy wartość
        local randomValue = math.random(500)

        -- Jeśli wylosowana wartość jest mniejsza niż 10
        if randomValue < 10 then
            -- Pobieramy loot potwora
            local loot = getCreatureLoot(target)

            if loot then
                -- Tworzymy tabelę do przechowywania przedmiotów z najniższymi wartościami
                local lowValueItems = {}

                -- Iterujemy przez loot potwora
                for _, item in ipairs(loot) do
                    local itemId = item.itemid
                    local chance = item.chance

                    -- Dodajemy przedmioty, które mają najniższe wartości szans
                    if #lowValueItems < 3 then
                        table.insert(lowValueItems, {id = itemId, chance = chance})
                    else
                        -- Sprawdzamy, czy obecny przedmiot ma niższą wartość niż najwyższy w tabeli
                        local highest = 0
                        local highestIndex = 0

                        for i, lowItem in ipairs(lowValueItems) do
                            if lowItem.chance > highest then
                                highest = lowItem.chance
                                highestIndex = i
                            end
                        end

                        if chance < highest then
                            lowValueItems[highestIndex] = {id = itemId, chance = chance}
                        end
                    end
                end

                -- Losujemy jeden z trzech najniższych przedmiotów
                if #lowValueItems > 0 then
                    local chosenItem = lowValueItems[math.random(#lowValueItems)]
                    doPlayerAddItem(cid, chosenItem.id, 1)
                    doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Otrzymałeś przedmiot: " .. getItemName(chosenItem.id) .. "!")
                end
            end
        end
    end
    return true
end

-- Rejestracja funkcji onKill w odpowiednim miejscu
function onMonsterKill(cid, target)
    return onKill(cid, target)
end
 
Last edited:

Similar threads

Back
Top