• 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!

CreatureEvent [TFS 1.x] Simple death broadcast channel

Roddet

Staff member
Global Moderator
Joined
May 1, 2013
Messages
928
Solutions
101
Reaction score
748
Location
Mex
Lua:
local subType = function(i)
    local ret = ""
    if(i <= 1) then
        ret = " was killed by "
    elseif(i > 1 and i <= 4) then
        ret = " was slain by "
    elseif(i > 4 and i <= 7) then
        ret = " was crushed by "
    elseif(i > 7 and i <= 10) then
        ret = " was eliminated by "
    elseif(i > 10) then
        ret = " was annihilated by "
    end
    return ret
end

function onDeath(player, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)

    local k = {}
    local channelId = 0
    local creatures = {}
    local deathList = player:getDamageMap()

    if next(deathList) then
        for creature, damage in pairs(deathList) do
            creature = creature >= 0x40000000 and Monster(creature) or Player(creature)

            if creature then
                if not creatures[creature:getName():lower()] then
                    if creature:getMaster() then
                        k[#k+1] = "a " .. creature:getName():lower() .. " summoned by " .. (creature:getMaster():isPlayer() and "" or "a ") .. creature:getMaster():getName()
                    elseif creature:isPlayer() then
                        k[#k+1] = creature:getName()
                    elseif creature:isMonster() then
                        k[#k+1] = "a " .. creature:getName():lower()
                    end
                    creatures[creature:getName():lower()] = 1
                end
            end
        end
        sendChannelMessage(channelId, TALKTYPE_CHANNEL_O, player:getName() .. subType(#k) .. table.concat(k, ", "):gsub("(.*),", "%1 and")..".")
    end
    return true
end
 
Last edited:
where can i add this

[".. player:getLevel() .."] ?


example:


Player1[100] was killed by Player2.
 
where can i add this: [".. player:getLevel() .."] ?

Lua:
sendChannelMessage(channelId, TALKTYPE_CHANNEL_O, player:getName() .. " [" .. player:getLevel() .. "]" .. subType(#k) .. table.concat(k, ", "):gsub("(.*),", "%1 and") .. ".")
 
Revscript version:

data/scripts/deathchannel.lua
Lua:
local config = {
    channelId = 9,

    subTypes = {
        { count = 1, message = 'was killed by' },
        { count = 2, message = 'was slain by' },
        { count = 5, message = 'was crushed by' },
        { count = 8, message = 'was eliminated by' },
        { count = 10, message = 'was annihilated by' }
    }
}

local creatureEvent = CreatureEvent("DeathChannelOnDeath")

function creatureEvent.onDeath(player)
    local attackersCount, description, creatures = 0, {}, {}
    for uid, cb in pairs(player:getDamageMap()) do
        local attacker = Creature(uid)
        if attacker then
            if attacker:isPlayer() then
                attackersCount = attackersCount + 1
                description[attackersCount] = attacker:getName()
            elseif attacker:isMonster() then
                local attackerName = attacker:getName()
                if not creatures[attackerName] then
                    local master = attacker:getMaster()
                    if master then
                        attackersCount = attackersCount + 1
                        description[attackersCount] = string.format('a %s summoned by %s', attackerName, master:isPlayer() and master:getName() or master:getType():getNameDescription())
                    else
                        attackersCount = attackersCount + 1
                        description[attackersCount] = string.format('a %s', attackerName)
                    end
                    creatures[attackerName] = true
                end
            end
        end
    end

    local subType = nil
    for _, info in pairs(config.subTypes) do
        if attackersCount >= info.count then
            subType = info.message
        end
    end

    sendChannelMessage(config.channelId, TALKTYPE_CHANNEL_O, string.format('%s [%d] %s %s.', player:getName(), player:getLevel(), subType, table.concat(description, ', '):gsub('(.*),', '%1 and')))
end

creatureEvent:register()

local creatureEvent = CreatureEvent("DeathChannelOnLogin")

function creatureEvent.onLogin(player)
    player:registerEvent("DeathChannelOnDeath")
    return true
end

creatureEvent:register()
 
Last edited:
Subtype will always be was killed by because it's checking if higher than 1, right?
Anyway to correct it with 1 statement or should I go with a few elseif?
Lua:
local subType = nil
    for count, subType in pairs(config.subTypes) do
        if attackersCount >= count then
            subType = subType
        end
    end
 
Subtype will always be was killed by because it's checking if higher than 1, right?
Anyway to correct it with 1 statement or should I go with a few elseif?
Lua:
local subType = nil
    for count, subType in pairs(config.subTypes) do
        if attackersCount >= count then
            subType = subType
        end
    end
if you decide to declare a message for each number, 1,2,3,4,5,6,7,8,9,10,11.... there is no need for this loop. you can just do config.subTypes[attackersCount]
And who said that count is always 1?
 
if you decide to declare a message for each number, 1,2,3,4,5,6,7,8,9,10,11.... there is no need for this loop. you can just do config.subTypes[attackersCount]
I thought the message should be declared for like
1 killer = was killed by
2 to 4 killers = was slain by
5 to 7 killers = was crushed by
etc.., or I understood it wrong?
And who said that count is always 1?
I have been testing it for a while and it never reaches the 2nd message, I haven't changed anything other than the message type to TALKTYPE_CHANNEL_Y.
Not always 1, But always >= 1 so it returns to killed by.
messagekilled.PNG
 
Last edited:
I thought the message should be declared for like
1 killer = was killed by
2 to 4 killers = was slain by
5 to 7 killers = was crushed by
etc.., or I understood it wrong?

I have been testing it for a while and it never reaches the 2nd message, I haven't changed anything other than the message type to TALKTYPE_CHANNEL_Y.
Not always 1, But always >= 1 so it returns to killed by.
View attachment 72494
I think you're right the table is not linear, I'll fix it in a second.
If the table were ordered it would work perfect.

Ready.
Thanks for reporting it, I don't usually try the codes and I'm wrong. <3

Lua is kind of crazy and doesn't order things as one declares them, but does it arbitrarily. So even though I declare 1, 2, 5, 8, 10, for Lua this doesn't have to be like that, when it compiles it can be in another order, in this case it seemed that [1] was last because the message always appears 1
 
Lua is kind of crazy and doesn't order things as one declares them, but does it arbitrarily. So even though I declare 1, 2, 5, 8, 10, for Lua this doesn't have to be like that, when it compiles it can be in another order, in this case it seemed that [1] was last because the message always appears 1
Not crazy tho, its just a way to improve performance same as <unordered_map>
 
Revscript version:

data/scripts/deathchannel.lua
Lua:
local config = {
    channelId = 9,

    subTypes = {
        { count = 1, message = 'was killed by' },
        { count = 2, message = 'was slain by' },
        { count = 5, message = 'was crushed by' },
        { count = 8, message = 'was eliminated by' },
        { count = 10, message = 'was annihilated by' }
    }
}

local creatureEvent = CreatureEvent("DeathChannelOnDeath")

function creatureEvent.onDeath(player)
    local attackersCount, description, creatures = 0, {}, {}
    for uid, cb in pairs(player:getDamageMap()) do
        local attacker = Creature(uid)
        if attacker then
            if attacker:isPlayer() then
                attackersCount = attackersCount + 1
                description[attackersCount] = attacker:getName()
            elseif attacker:isMonster() then
                local attackerName = attacker:getName()
                if not creatures[attackerName] then
                    local master = attacker:getMaster()
                    if master then
                        attackersCount = attackersCount + 1
                        description[attackersCount] = string.format('a %s summoned by %s', attackerName, master:isPlayer() and master:getName() or master:getType():getNameDescription())
                    else
                        attackersCount = attackersCount + 1
                        description[attackersCount] = string.format('a %s', attackerName)
                    end
                    creatures[attackerName] = true
                end
            end
        end
    end

    local subType = nil
    for _, info in pairs(config.subTypes) do
        if attackersCount >= info.count then
            subType = info.message
        end
    end

    sendChannelMessage(config.channelId, TALKTYPE_CHANNEL_O, string.format('%s [%d] %s %s.', player:getName(), player:getLevel(), subType, table.concat(description, ', '):gsub('(.*),', '%1 and')))
end

creatureEvent:register()

local creatureEvent = CreatureEvent("DeathChannelOnLogin")

function creatureEvent.onLogin(player)
    player:registerEvent("DeathChannelOnDeath")
    return true
end

creatureEvent:register()
I tried to use your script, but don't send messages on the channel.

chatchannels.xml
XML:
    <channel id="13" name="Death Channel" public="1" script="deathchannel.lua" />

chatchannels/scripts/deathchannel.lua
Lua:
function onSpeak(player, type, message)
    local playerAccountType = player:getAccountType()
    if playerAccountType < ACCOUNT_TYPE_GAMEMASTER then
        player:sendCancelMessage("You can't talk on this channel.")
        return false
    end
    return type
end

revscripts
scripts/deathchannel.lua
Lua:
local config = {
    channelId = 13,

    subTypes = {
        { count = 1, message = 'was killed by' },
        { count = 2, message = 'was slain by' },
        { count = 5, message = 'was crushed by' },
        { count = 8, message = 'was eliminated by' },
        { count = 10, message = 'was annihilated by' }
    }
}

local creatureEvent = CreatureEvent("DeathChannelOnDeath")

function creatureEvent.onDeath(player)
    local attackersCount, description, creatures = 0, {}, {}
    for uid, cb in pairs(player:getDamageMap()) do
        local attacker = Creature(uid)
        if attacker then
            if attacker:isPlayer() then
                attackersCount = attackersCount + 1
                description[attackersCount] = attacker:getName()
            elseif attacker:isMonster() then
                local attackerName = attacker:getName()
                if not creatures[attackerName] then
                    local master = attacker:getMaster()
                    if master then
                        attackersCount = attackersCount + 1
                        description[attackersCount] = string.format('a %s summoned by %s', attackerName, master:isPlayer() and master:getName() or master:getType():getNameDescription())
                    else
                        attackersCount = attackersCount + 1
                        description[attackersCount] = string.format('a %s', attackerName)
                    end
                    creatures[attackerName] = true
                end
            end
        end
    end

    local subType = nil
    for _, info in pairs(config.subTypes) do
        if attackersCount >= info.count then
            subType = info.message
        end
    end

    sendChannelMessage(config.channelId, TALKTYPE_CHANNEL_O, string.format('%s [%d] %s %s.', player:getName(), player:getLevel(), subType, table.concat(description, ', '):gsub('(.*),', '%1 and')))
end

creatureEvent:register()

local creatureEvent = CreatureEvent("DeathChannelOnLogin")

function creatureEvent.onLogin(player)
    player:registerEvent("DeathChannelOnDeath")
    return true
end

creatureEvent:register()

There are no errors in the distro.

Source
Code:
https://github.com/nekiro/TFS-1.5-Downgrades/tree/7.72
 
I tried to use your script, but don't send messages on the channel.

chatchannels.xml
XML:
    <channel id="13" name="Death Channel" public="1" script="deathchannel.lua" />

chatchannels/scripts/deathchannel.lua
Lua:
function onSpeak(player, type, message)
    local playerAccountType = player:getAccountType()
    if playerAccountType < ACCOUNT_TYPE_GAMEMASTER then
        player:sendCancelMessage("You can't talk on this channel.")
        return false
    end
    return type
end

revscripts
scripts/deathchannel.lua
Lua:
local config = {
    channelId = 13,

    subTypes = {
        { count = 1, message = 'was killed by' },
        { count = 2, message = 'was slain by' },
        { count = 5, message = 'was crushed by' },
        { count = 8, message = 'was eliminated by' },
        { count = 10, message = 'was annihilated by' }
    }
}

local creatureEvent = CreatureEvent("DeathChannelOnDeath")

function creatureEvent.onDeath(player)
    local attackersCount, description, creatures = 0, {}, {}
    for uid, cb in pairs(player:getDamageMap()) do
        local attacker = Creature(uid)
        if attacker then
            if attacker:isPlayer() then
                attackersCount = attackersCount + 1
                description[attackersCount] = attacker:getName()
            elseif attacker:isMonster() then
                local attackerName = attacker:getName()
                if not creatures[attackerName] then
                    local master = attacker:getMaster()
                    if master then
                        attackersCount = attackersCount + 1
                        description[attackersCount] = string.format('a %s summoned by %s', attackerName, master:isPlayer() and master:getName() or master:getType():getNameDescription())
                    else
                        attackersCount = attackersCount + 1
                        description[attackersCount] = string.format('a %s', attackerName)
                    end
                    creatures[attackerName] = true
                end
            end
        end
    end

    local subType = nil
    for _, info in pairs(config.subTypes) do
        if attackersCount >= info.count then
            subType = info.message
        end
    end

    sendChannelMessage(config.channelId, TALKTYPE_CHANNEL_O, string.format('%s [%d] %s %s.', player:getName(), player:getLevel(), subType, table.concat(description, ', '):gsub('(.*),', '%1 and')))
end

creatureEvent:register()

local creatureEvent = CreatureEvent("DeathChannelOnLogin")

function creatureEvent.onLogin(player)
    player:registerEvent("DeathChannelOnDeath")
    return true
end

creatureEvent:register()

There are no errors in the distro.

Source
Code:
https://github.com/nekiro/TFS-1.5-Downgrades/tree/7.72
same problem
 
does anybody know why there is [0] at the beginning of the message? Seems like level of the channel ;? XD is there a way to remove it?

1710115594202.png
 
does anybody know why there is [0] at the beginning of the message? Seems like level of the channel ;? XD is there a way to remove it?

View attachment 82906
It took me about 4 hours to make it the way I wanted, try it if you like it.
Lua:
local config = {
    channelId = 5,

    subTypes = {
        { count = 1, message = 'was killed by:' },
        { count = 2, message = 'was devoured by:' },
        { count = 3, message = 'was executed by:' },
        { count = 5, message = 'was demolished by:' },
        { count = 8, message = 'was sentenced by:' },
        { count = 10, message = 'was slaughtered by:' }
    }
}

local creatureEvent = CreatureEvent("DeathChannelOnDeath")

function creatureEvent.onDeath(player)
    local attackersCount, description, creatures = 0, {}, {}

    for uid, cb in pairs(player:getDamageMap()) do
        local attacker = Creature(uid)
        if attacker then
            if attacker:isPlayer() then
                attackersCount = attackersCount + 1
                table.insert(description, { type = "player", name = attacker:getName(), level = attacker:getLevel() })
            elseif attacker:isMonster() then
                local attackerName = attacker:getName()
                if not creatures[attackerName] then
                    local master = attacker:getMaster()
                    if master then
                        attackersCount = attackersCount + 1
                        table.insert(description, { type = "monster", name = attackerName, level = nil, master = master:getName(), count = 1 })
                    else
                        attackersCount = attackersCount + 1
                        table.insert(description, { type = "monster", name = attackerName, level = nil, master = nil, count = 1 })
                    end
                    creatures[attackerName] = #description
                else
                    description[creatures[attackerName]].count = description[creatures[attackerName]].count + 1
                end
            end
        end
    end

    local subType = nil
    local channelType = TALKTYPE_CHANNEL_O -- Normal Channel

    for i, info in ipairs(config.subTypes) do
        if attackersCount >= info.count and (i == #config.subTypes or attackersCount < config.subTypes[i+1].count) then
            subType = info.message
            break
        end
    end

    if subType == nil then
        subType = config.subTypes[#config.subTypes].message
    end

    if attackersCount >= 5 then
        channelType = TALKTYPE_CHANNEL_R1
    elseif attackersCount >= 2 then
        channelType = TALKTYPE_CHANNEL_Y
    end

    table.sort(description, function(a, b)
        return a.type == "monster" and b.type == "player"
    end)

    local attackerInfo = ""
    for i, desc in ipairs(description) do
        if desc.type == "player" then
            if i == attackersCount and i > 1 then
                attackerInfo = attackerInfo .. " and"
            elseif i > 1 then
                attackerInfo = attackerInfo .. ","
            end
            attackerInfo = attackerInfo .. string.format(" %s [%d]", desc.name, desc.level)
        elseif desc.type == "monster" then
            if i == attackersCount and i > 1 then
                attackerInfo = attackerInfo .. " and"
            elseif i > 1 then
                attackerInfo = attackerInfo .. ","
            end
            local monsterInfo = desc.count >= 2 and string.format(" %sx %s", desc.count, desc.name) or string.format(" %s", desc.name)
            if desc.master then
                monsterInfo = monsterInfo .. string.format(" summoned by %s", desc.master)
            end
            attackerInfo = attackerInfo .. monsterInfo
        end
    end

    local totalAttackersMessage = attackersCount > 1 and string.format(" (%d attackers)", attackersCount) or ""

    if attackersCount > 1 then
        totalAttackersMessage = string.format(" (%d attackers)", attackersCount)
    end

    local channelMessage = string.format('%s [%d] %s%s%s.', player:getName(), player:getLevel(), subType, attackerInfo, totalAttackersMessage)

    local guild = player:getGuild()
    sendChannelMessage(config.channelId, channelType, channelMessage)
    if guild then
    sendGuildChannelMessage(guild:getId(), channelType, channelMessage)
    end
end

creatureEvent:register()

local creatureEvent = CreatureEvent("DeathChannelOnLogin")

function creatureEvent.onLogin(player)
    player:registerEvent("DeathChannelOnDeath")
    return true
end

creatureEvent:register()
 
It took me about 4 hours to make it the way I wanted, try it if you like it.
Lua:
local config = {
    channelId = 5,

    subTypes = {
        { count = 1, message = 'was killed by:' },
        { count = 2, message = 'was devoured by:' },
        { count = 3, message = 'was executed by:' },
        { count = 5, message = 'was demolished by:' },
        { count = 8, message = 'was sentenced by:' },
        { count = 10, message = 'was slaughtered by:' }
    }
}

local creatureEvent = CreatureEvent("DeathChannelOnDeath")

function creatureEvent.onDeath(player)
    local attackersCount, description, creatures = 0, {}, {}

    for uid, cb in pairs(player:getDamageMap()) do
        local attacker = Creature(uid)
        if attacker then
            if attacker:isPlayer() then
                attackersCount = attackersCount + 1
                table.insert(description, { type = "player", name = attacker:getName(), level = attacker:getLevel() })
            elseif attacker:isMonster() then
                local attackerName = attacker:getName()
                if not creatures[attackerName] then
                    local master = attacker:getMaster()
                    if master then
                        attackersCount = attackersCount + 1
                        table.insert(description, { type = "monster", name = attackerName, level = nil, master = master:getName(), count = 1 })
                    else
                        attackersCount = attackersCount + 1
                        table.insert(description, { type = "monster", name = attackerName, level = nil, master = nil, count = 1 })
                    end
                    creatures[attackerName] = #description
                else
                    description[creatures[attackerName]].count = description[creatures[attackerName]].count + 1
                end
            end
        end
    end

    local subType = nil
    local channelType = TALKTYPE_CHANNEL_O -- Normal Channel

    for i, info in ipairs(config.subTypes) do
        if attackersCount >= info.count and (i == #config.subTypes or attackersCount < config.subTypes[i+1].count) then
            subType = info.message
            break
        end
    end

    if subType == nil then
        subType = config.subTypes[#config.subTypes].message
    end

    if attackersCount >= 5 then
        channelType = TALKTYPE_CHANNEL_R1
    elseif attackersCount >= 2 then
        channelType = TALKTYPE_CHANNEL_Y
    end

    table.sort(description, function(a, b)
        return a.type == "monster" and b.type == "player"
    end)

    local attackerInfo = ""
    for i, desc in ipairs(description) do
        if desc.type == "player" then
            if i == attackersCount and i > 1 then
                attackerInfo = attackerInfo .. " and"
            elseif i > 1 then
                attackerInfo = attackerInfo .. ","
            end
            attackerInfo = attackerInfo .. string.format(" %s [%d]", desc.name, desc.level)
        elseif desc.type == "monster" then
            if i == attackersCount and i > 1 then
                attackerInfo = attackerInfo .. " and"
            elseif i > 1 then
                attackerInfo = attackerInfo .. ","
            end
            local monsterInfo = desc.count >= 2 and string.format(" %sx %s", desc.count, desc.name) or string.format(" %s", desc.name)
            if desc.master then
                monsterInfo = monsterInfo .. string.format(" summoned by %s", desc.master)
            end
            attackerInfo = attackerInfo .. monsterInfo
        end
    end

    local totalAttackersMessage = attackersCount > 1 and string.format(" (%d attackers)", attackersCount) or ""

    if attackersCount > 1 then
        totalAttackersMessage = string.format(" (%d attackers)", attackersCount)
    end

    local channelMessage = string.format('%s [%d] %s%s%s.', player:getName(), player:getLevel(), subType, attackerInfo, totalAttackersMessage)

    local guild = player:getGuild()
    sendChannelMessage(config.channelId, channelType, channelMessage)
    if guild then
    sendGuildChannelMessage(guild:getId(), channelType, channelMessage)
    end
end

creatureEvent:register()

local creatureEvent = CreatureEvent("DeathChannelOnLogin")

function creatureEvent.onLogin(player)
    player:registerEvent("DeathChannelOnDeath")
    return true
end

creatureEvent:register()

Since you helped me I will help you.


Lua:
local creatureEvent = CreatureEvent("DeathChannelOnLogin")

function creatureEvent.onLogin(player)
    player:registerEvent("DeathChannelOnDeath")

    local deathChannelId = config.channelId
    player:openChannel(deathChannelId)

    return true
end

creatureEvent:register()

This is how it looks now:

1710164114178.png
 
Back
Top