• 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+ Monster counter tfs 1.5 8.6

granitowyx

New Member
Joined
Jan 27, 2022
Messages
20
Reaction score
0
GitHub
rupafin123
Hello. I need some help. I use tfs 1.5 downgrade 8.6 by Nekiro. I have creaturescript that count killed monsters. It works fine but this is only counted for players who deal most dmg and last hit. I want to count monsters for every player who deal dmg. Script:
Lua:
local monstersToCount = {
-- amazons
    ["amazon"] = {storage = 1000, groupStorage = 5000},
    ["valkyrie"] = {storage = 1001, groupStorage = 5000}
    
function onKill(creature, target)
    if not Player(creature) then --check if killer is a Player
        return true
    end
    
    if not Monster(target) then --check if killed is a Monster
        return true
    end
    
    local monsterIndex = monstersToCount[string.lower(target:getName())] --get monster info from table using string.lower() to change target name to low-case
    if monsterIndex then --if exist monster info
        --update individual count
        local monsterKilledCount = creature:getStorageValue(monsterIndex.storage)
        if monsterKilledCount < 0 then--check if individual count is lower than 0 (default storage value = -1) and set to 0
            creature:setStorageValue(monsterIndex.storage, 0)
            monsterKilledCount = 0
        end
        creature:setStorageValue(monsterIndex.storage, monsterKilledCount + 1)
        --update group count
        local monsterGroupKilledCount = creature:getStorageValue(monsterIndex.groupStorage)
        if monsterGroupKilledCount < 0 then--check if group count is lower than 0 (default storage value = -1) and set to 0
            creature:setStorageValue(monsterIndex.groupStorage, 0)
            monsterGroupKilledCount = 0
        end
        creature:setStorageValue(monsterIndex.groupStorage, monsterGroupKilledCount + 1)
            if monsterKilledCount % 10 == 0
            then
            creature:sendTextMessage(MESSAGE_EVENT_ORANGE, "You killed "..(monsterKilledCount + 1).." "..target:getName().." and "..(monsterGroupKilledCount + 1).." monsters of this group.")
            end
    end
    return true
end
 
You need to change event onKill to onDeath then

you have to use :

Lua:
local map = creature:getDamageMap()
   for pid, damage in pairs(map) do
       local player = Player(pid)
       player:setStorageValue(monsterIndex.groupStorage, monsterGroupKilledCount + 1)
   end
 
You need to change event onKill to onDeath then

you have to use :

Lua:
local map = creature:getDamageMap()
   for pid, damage in pairs(map) do
       local player = Player(pid)
       player:setStorageValue(monsterIndex.groupStorage, monsterGroupKilledCount + 1)
   end
Sorry but how to remake my scripts with this function
 
Something like this. Not tested:
Lua:
local monstersToCount = {
    -- amazons
    ["amazon"] = { storage = 1000, groupStorage = 5000 },
    ["valkyrie"] = { storage = 1001, groupStorage = 5000 },
}

local function updateKills(creature, target, monsterIndex)
    --update individual count
    local monsterKilledCount = creature:getStorageValue(monsterIndex.storage)
    if monsterKilledCount < 0 then
        --check if individual count is lower than 0 (default storage value = -1) and set to 0
        creature:setStorageValue(monsterIndex.storage, 0)
        monsterKilledCount = 0
    end
    creature:setStorageValue(monsterIndex.storage, monsterKilledCount + 1)

    --update group count
    local monsterGroupKilledCount = creature:getStorageValue(monsterIndex.groupStorage)
    if monsterGroupKilledCount < 0 then
        --check if group count is lower than 0 (default storage value = -1) and set to 0
        creature:setStorageValue(monsterIndex.groupStorage, 0)
        monsterGroupKilledCount = 0
    end
    creature:setStorageValue(monsterIndex.groupStorage, monsterGroupKilledCount + 1)

    if monsterKilledCount % 10 == 0 then
        creature:sendTextMessage(MESSAGE_EVENT_ORANGE, "You killed " .. (monsterKilledCount + 1) .. " " .. target:getName() .. " and " .. (monsterGroupKilledCount + 1) .. " monsters of this group.")
    end
end

local lastKilledCreature = 0
function onKill(creature, target)
    if not Player(creature) then
        --check if killer is a Player
        return true
    end

    if not Monster(target) then
        --check if killed is a Monster
        return true
    end

    -- [!!!] onKill event can be execute 1 or 2 times [for mostDamageKiller and lastHitKiller]
    -- prevent double kill counting by storing last event 'target' in variable
    if lastKilledCreature == target:getId() then
        return true
    end
    lastKilledCreature = target:getId()

    --get monster info from table using string.lower() to change target name to low-case
    local monsterIndex = monstersToCount[string.lower(target:getName())]
    if monsterIndex then
        local killersMap = target:getDamageMap()

        for killerId, damage in pairs(killersMap) do
            local killer = Player(killerId)
            if killer then
                updateKills(killer, target, monsterIndex)
            end
        end
    end

    return true
end

Problem with getStorageValue that returns -1 you can fix using math.max with 0 as one parameter.
Your code with if:
Lua:
    local monsterKilledCount = creature:getStorageValue(monsterIndex.storage)
    if monsterKilledCount < 0 then
        --check if individual count is lower than 0 (default storage value = -1) and set to 0
        creature:setStorageValue(monsterIndex.storage, 0)
        monsterKilledCount = 0
    end
    creature:setStorageValue(monsterIndex.storage, monsterKilledCount + 1)
Version with math.max:
Lua:
local monsterKilledCount = math.max(0, creature:getStorageValue(monsterIndex.storage))
creature:setStorageValue(monsterIndex.storage, monsterKilledCount + 1)

Your code with math.max and + 1 moved to variable definition:
Lua:
local monstersToCount = {
    -- amazons
    ["amazon"] = { storage = 1000, groupStorage = 5000 },
    ["valkyrie"] = { storage = 1001, groupStorage = 5000 },
}

local function updateKills(creature, target, monsterIndex)
    --update individual count
    local monsterKilledCount = math.max(0, creature:getStorageValue(monsterIndex.storage)) + 1
    creature:setStorageValue(monsterIndex.storage, monsterKilledCount)

    --update group count
    local monsterGroupKilledCount = math.max(0, creature:getStorageValue(monsterIndex.groupStorage)) + 1
    creature:setStorageValue(monsterIndex.groupStorage, monsterGroupKilledCount)

    if monsterKilledCount % 10 == 0 then
        creature:sendTextMessage(MESSAGE_EVENT_ORANGE, "You killed " .. (monsterKilledCount) .. " " .. target:getName() .. " and " .. (monsterGroupKilledCount) .. " monsters of this group.")
    end
end

local lastKilledCreature = 0

function onKill(creature, target)
    if not Player(creature) then
        --check if killer is a Player
        return true
    end

    if not Monster(target) then
        --check if killed is a Monster
        return true
    end


    -- [!!!] onKill event can be execute 1 or 2 times [for mostDamageKiller and lastHitKiller]
    -- prevent double kill counting by storing last event 'target' in variable
    if lastKilledCreature == target:getId() then
        return true
    end
    lastKilledCreature = target:getId()

    --get monster info from table using string.lower() to change target name to low-case
    local monsterIndex = monstersToCount[string.lower(target:getName())]
    if monsterIndex then
        local killersMap = target:getDamageMap()

        for killerId, damage in pairs(killersMap) do
            local killer = Player(killerId)
            if killer then
                updateKills(killer, target, monsterIndex)
            end
        end
    end

    return true
end
 
Last edited:
Something like this. Not tested:
Lua:
local monstersToCount = {
    -- amazons
    ["amazon"] = { storage = 1000, groupStorage = 5000 },
    ["valkyrie"] = { storage = 1001, groupStorage = 5000 },
}

local function updateKills(creature, target, monsterIndex)
    --update individual count
    local monsterKilledCount = creature:getStorageValue(monsterIndex.storage)
    if monsterKilledCount < 0 then
        --check if individual count is lower than 0 (default storage value = -1) and set to 0
        creature:setStorageValue(monsterIndex.storage, 0)
        monsterKilledCount = 0
    end
    creature:setStorageValue(monsterIndex.storage, monsterKilledCount + 1)

    --update group count
    local monsterGroupKilledCount = creature:getStorageValue(monsterIndex.groupStorage)
    if monsterGroupKilledCount < 0 then
        --check if group count is lower than 0 (default storage value = -1) and set to 0
        creature:setStorageValue(monsterIndex.groupStorage, 0)
        monsterGroupKilledCount = 0
    end
    creature:setStorageValue(monsterIndex.groupStorage, monsterGroupKilledCount + 1)

    if monsterKilledCount % 10 == 0 then
        creature:sendTextMessage(MESSAGE_EVENT_ORANGE, "You killed " .. (monsterKilledCount + 1) .. " " .. target:getName() .. " and " .. (monsterGroupKilledCount + 1) .. " monsters of this group.")
    end
end

local lastKilledCreature = 0
function onKill(creature, target)
    if not Player(creature) then
        --check if killer is a Player
        return true
    end

    if not Monster(target) then
        --check if killed is a Monster
        return true
    end

    -- [!!!] onKill event can be execute 1 or 2 times [for mostDamageKiller and lastHitKiller]
    -- prevent double kill counting by storing last event 'target' in variable
    if lastKilledCreature == target:getId() then
        return true
    end
    lastKilledCreature = target:getId()

    --get monster info from table using string.lower() to change target name to low-case
    local monsterIndex = monstersToCount[string.lower(target:getName())]
    if monsterIndex then
        local killersMap = target:getDamageMap()

        for killerId, damage in pairs(killersMap) do
            local killer = Player(killerId)
            if killer then
                updateKills(killer, target, monsterIndex)
            end
        end
    end

    return true
end

Problem with getStorageValue that returns -1 you can fix using math.max with 0 as one parameter.
Your code with if:
Lua:
    local monsterKilledCount = creature:getStorageValue(monsterIndex.storage)
    if monsterKilledCount < 0 then
        --check if individual count is lower than 0 (default storage value = -1) and set to 0
        creature:setStorageValue(monsterIndex.storage, 0)
        monsterKilledCount = 0
    end
    creature:setStorageValue(monsterIndex.storage, monsterKilledCount + 1)
Version with math.max:
Lua:
local monsterKilledCount = math.max(0, creature:getStorageValue(monsterIndex.storage))
creature:setStorageValue(monsterIndex.storage, monsterKilledCount + 1)

Your code with math.max and + 1 moved to variable definition:
Lua:
local monstersToCount = {
    -- amazons
    ["amazon"] = { storage = 1000, groupStorage = 5000 },
    ["valkyrie"] = { storage = 1001, groupStorage = 5000 },
}

local function updateKills(creature, target, monsterIndex)
    --update individual count
    local monsterKilledCount = math.max(0, creature:getStorageValue(monsterIndex.storage)) + 1
    creature:setStorageValue(monsterIndex.storage, monsterKilledCount)

    --update group count
    local monsterGroupKilledCount = math.max(0, creature:getStorageValue(monsterIndex.groupStorage)) + 1
    creature:setStorageValue(monsterIndex.groupStorage, monsterGroupKilledCount)

    if monsterKilledCount % 10 == 0 then
        creature:sendTextMessage(MESSAGE_EVENT_ORANGE, "You killed " .. (monsterKilledCount) .. " " .. target:getName() .. " and " .. (monsterGroupKilledCount) .. " monsters of this group.")
    end
end

local lastKilledCreature = 0

function onKill(creature, target)
    if not Player(creature) then
        --check if killer is a Player
        return true
    end

    if not Monster(target) then
        --check if killed is a Monster
        return true
    end


    -- [!!!] onKill event can be execute 1 or 2 times [for mostDamageKiller and lastHitKiller]
    -- prevent double kill counting by storing last event 'target' in variable
    if lastKilledCreature == target:getId() then
        return true
    end
    lastKilledCreature = target:getId()

    --get monster info from table using string.lower() to change target name to low-case
    local monsterIndex = monstersToCount[string.lower(target:getName())]
    if monsterIndex then
        local killersMap = target:getDamageMap()

        for killerId, damage in pairs(killersMap) do
            local killer = Player(killerId)
            if killer then
                updateKills(killer, target, monsterIndex)
            end
        end
    end

    return true
end
Works fine! Thank you
 
Back
Top