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

Reward when killing a player

try adding this to creaturescripts.xml:
<event type="kill" name="KilledSomething" script="MyKillScript.lua"/>

then implement the script "MyKillScript.lua" with callback API "function onKill(cid, target, lastHit)" in
\data\creaturescripts\scripts

I haven't done this myself, but I have an example from a server that shares party loot this way.

AFAIK the event type has to be "kill", but you can name the other two whatever you like.
 
<?xml version="1.0" encoding="UTF-8"?>
<creaturescripts>
<event type="login" name="PlayerLogin" script="login.lua"/>
<event type="login" name="FirstItems" script="firstitems.lua"/>
<event type="death" name="PlayerDeath" script="playerdeath.lua"/>
<event type="extendedopcode" name="ExtendedOpcode" script="extendedopcode.lua"/>
<event type="kill" name="RewardFrag" script="rewardfrag.lua"/>
<event type="login" name="Bless" script="bless.lua"/>
</creaturescripts>


NOTE, the rewardfrag does not work


PHP:
function onKill(cid, target, lastHit)
    if(lastHit and isPlayer(target) and getPlayerLevel(target) >= 120) then
        doItemSetAttribute(doPlayerAddItem(cid, 2157, 1), "description", "It's a trophy you gained for killing " .. getCreatureName(target) .. " when " .. (getPlayerSex(target) == 0 and "she" or "he") .. " was level " .. getPlayerLevel(target) .. ".")
    end
    return true
end
 
try if this one works:
PHP:
function onKill(cid, target, lastHit)
local gender_name = {[0] = "she", [1] = "he"}
    if isPlayer(target) and getPlayerLevel(target) >= 120 then
        trophy = doPlayerAddItem(cid, 2157, 1)
        doSetItemSpecialDescription(trophy, "It's a trophy you gained for killing " .. getCreatureName(target) .. " when " .. gender_name[getPlayerSex(target)] .. " was level " .. getPlayerLevel(target) .. ".")
    end
    return true
end

Make sure you have registered "RewardFrag" in login.lua before testing!
 
Code:
data/creaturescripts/scripts/playerdeath.lua:onDeath
data/creaturescripts/scripts/playerdeath.lua:73: attempt to index a nil value
stack traceback:
        [C]: in function '__index'
        data/creaturescripts/scripts/playerdeath.lua:73: in function <data/creaturescripts/scripts/playerdeath.lua:4>


Code:
    player:registerEvent("PlayerDeath")
    player:registerEvent("RewardFrag")

up
 
Last edited by a moderator:
Code:
local deathListEnabled = true
local maxDeathRecords = 5

function onDeath(cid, corpse, killer, mostDamage, unjustified, mostDamage_unjustified)
    local player = Player(cid)

    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You are dead.")
    if  getPlayerLevel(cid) < 400 then
        doSetCreatureDropLoot(cid, false)
    end
    if not deathListEnabled then
        return
    end

    local byPlayer = 0
    local killerCreature = Creature(killer)
    if killerCreature == nil then
        killerName = "field item"
    else
        if killerCreature:isPlayer() then
            byPlayer = 1
        else
            local master = killerCreature:getMaster()
            if master and master ~= killerCreature and master:isPlayer() then
                killerCreature = master
                byPlayer = 1
            end
        end
        killerName = killerCreature:getName()
    end

    local byPlayerMostDamage = 0
    if mostDamage == 0 then
        mostDamageName = "field item"
    else
        local mostDamageKiller = Creature(mostDamage)
        if mostDamageKiller:isPlayer() then
            byPlayerMostDamage = 1
        else
            local master = mostDamageKiller:getMaster()
            if master and master ~= mostDamageKiller and master:isPlayer() then
                mostDamageKiller = master
                byPlayerMostDamage = 1
            end
        end
        mostDamageName = mostDamageKiller:getName()
    end

    local playerGuid = player:getGuid()
    db.query("INSERT INTO `player_deaths` (`player_id`, `time`, `level`, `killed_by`, `is_player`, `mostdamage_by`, `mostdamage_is_player`, `unjustified`, `mostdamage_unjustified`) VALUES (" .. playerGuid .. ", " .. os.time() .. ", " .. player:getLevel() .. ", " .. db.escapeString(killerName) .. ", " .. byPlayer .. ", " .. db.escapeString(mostDamageName) .. ", " .. byPlayerMostDamage .. ", " .. unjustified .. ", " .. mostDamage_unjustified .. ")")
    local resultId = db.storeQuery("SELECT `player_id` FROM `player_deaths` WHERE `player_id` = " .. playerGuid)

    local deathRecords = 0
    local tmpResultId = resultId
    while tmpResultId ~= false do
        tmpResultId = result.next(resultId)
        deathRecords = deathRecords + 1
    end

    if resultId ~= false then
        result.free(resultId)
    end

    while deathRecords > maxDeathRecords do
        db.query("DELETE FROM `player_deaths` WHERE `player_id` = " .. playerGuid .. " ORDER BY `time` LIMIT 1")
        deathRecords = deathRecords - 1
    end

    if byPlayer == 1 then
        local guild = player:getGuild()
        local targetGuild = guild and guild:getId()
        if targetGuild ~= 0 then
            local killerGuild = killerCreature:getGuild():getId()
            if killerGuild ~= 0 and targetGuild ~= killerGuild and isInWar(cid, killerCreature) then
                local warId = false
                resultId = db.storeQuery("SELECT `id` FROM `guild_wars` WHERE `status` = 1 AND ((`guild1` = " .. killerGuild .. " AND `guild2` = " .. targetGuild .. ") OR (`guild1` = " .. targetGuild .. " AND `guild2` = " .. killerGuild .. "))")
                if resultId ~= false then
                    warId = result.getDataInt(resultId, "id")
                    result.free(resultId)
                end

                if warId ~= false then
                    db.query("INSERT INTO `guildwar_kills` (`killer`, `target`, `killerguild`, `targetguild`, `time`, `warid`) VALUES (" .. db.escapeString(killerName) .. ", " .. db.escapeString(player:getName()) .. ", " .. killerGuild .. ", " .. targetGuild .. ", " .. os.time() .. ", " .. warId .. ")")
                end
            end
        end
    end
end
 
Back
Top