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

Lua [TFS 1.5 7.72] Redskull after kill players dont get ban

popa45

Member
Joined
Oct 14, 2010
Messages
75
Reaction score
9
Location
Brazil
GitHub
vilelafred
When the player kills more than 6 players, he is not banned; he simply logs out of the game and can play again. I have already searched everywhere on the forum and have not found a solution.


Lua:
local deathListEnabled = true
local maxDeathRecords = 5
local killsToBan = 6
local killsBanLenght = 3

function onDeath(player, corpse, killer, mostDamageKiller, unjustified, mostDamageUnjustified)
    local playerId = player:getId()
    if nextUseStaminaTime[playerId] ~= nil then
        nextUseStaminaTime[playerId] = nil
    end

    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You are dead.")

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

    local byPlayerMostDamage = 0
    local mostDamageKillerName
    if mostDamageKiller ~= nil then
        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()
    else
        mostDamageName = "field item"
    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 and 1 or 0) .. ", " .. (mostDamageUnjustified and 1 or 0) .. ")")
    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

    local limit = deathRecords - maxDeathRecords
    if limit > 0 then
        db.asyncQuery("DELETE FROM `player_deaths` WHERE `player_id` = " .. playerGuid .. " ORDER BY `time` LIMIT " .. limit)
    end

    if byPlayer == 1 then
        local targetGuild = player:getGuild()
        targetGuild = targetGuild and targetGuild:getId() or 0
        if targetGuild ~= 0 then
            local killerGuild = killer:getGuild()
            killerGuild = killerGuild and killerGuild:getId() or 0
            if killerGuild ~= 0 and targetGuild ~= killerGuild and isInWar(playerId, killer:getId()) 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.asyncQuery("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

        local skullTime = killer:getSkullTime()
        if skullTime > 0 then
            local kills = math.ceil(skullTime / configManager.getNumber(configKeys.FRAG_TIME))
            if kills >= killsToBan then
                local timeNow = os.time()
                db.query("INSERT INTO `account_bans` (`account_id`, `reason`, `banned_at`, `expires_at`, `banned_by`) VALUES (" .. killer:getAccountId() .. ", " .. db.escapeString("Excessive Unjustified Player Killing.") .. ", " .. timeNow .. ", " .. timeNow + (killsBanLenght * 86400) .. ", 1)")
                killer:getPosition():sendMagicEffect(CONST_ME_MAGIC_RED)
                killer:remove()
            end
        end
    end
end


2023-12-22 15:26:24 - [Error - mysql_real_query] Query: INSERT INTO account_bans (account_id, reason, banned_at, expires_at, banned_by) VALUES (320, 'Excessive Unjustified Player Killing.', 1703269584, 1703528784, 268435461)
2023-12-22 15:26:24 - Message: Cannot add or update a child row: a foreign key constraint fails (karma.account_bans, CONSTRAINT account_bans_ibfk_2 FOREIGN KEY (banned_by) REFERENCES players (id) ON DELETE CASCADE ON UPDATE CASCADE)
 
Solution
It seems like an implicit casting issue

You can try the following, on this query
Code:
db.query("INSERT INTO `account_bans` (`account_id`, `reason`, `banned_at`, `expires_at`, `banned_by`) VALUES (" .. killer:getAccountId() .. ", " .. db.escapeString("Excessive Unjustified Player Killing.") .. ", " .. timeNow .. ", " .. timeNow + (killsBanLenght * 86400) .. ", 1)")

Change the 1 to " .. tonumber(1) .. "
Also make sure that row id 1 exists on players table
It seems like an implicit casting issue

You can try the following, on this query
Code:
db.query("INSERT INTO `account_bans` (`account_id`, `reason`, `banned_at`, `expires_at`, `banned_by`) VALUES (" .. killer:getAccountId() .. ", " .. db.escapeString("Excessive Unjustified Player Killing.") .. ", " .. timeNow .. ", " .. timeNow + (killsBanLenght * 86400) .. ", 1)")

Change the 1 to " .. tonumber(1) .. "
Also make sure that row id 1 exists on players table
 
Solution
Lua:
local deathListEnabled = true
local maxDeathRecords = 5
local killsToBan = 5
local killsBanLenght = 3

function onDeath(player, corpse, killer, mostDamageKiller, unjustified, mostDamageUnjustified)
    local playerId = player:getId()
    if nextUseStaminaTime[playerId] ~= nil then
        nextUseStaminaTime[playerId] = nil
    end

    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You are dead.")

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

    local byPlayerMostDamage = 0
    local mostDamageKillerName
    if mostDamageKiller ~= nil then
        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()
    else
        mostDamageName = "field item"
    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 and 1 or 0) .. ", " .. (mostDamageUnjustified and 1 or 0) .. ")")
    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

    local limit = deathRecords - maxDeathRecords
    if limit > 0 then
        db.asyncQuery("DELETE FROM `player_deaths` WHERE `player_id` = " .. playerGuid .. " ORDER BY `time` LIMIT " .. limit)
    end

    if byPlayer == 1 then
        local targetGuild = player:getGuild()
        targetGuild = targetGuild and targetGuild:getId() or 0
        if targetGuild ~= 0 then
            local killerGuild = killer:getGuild()
            killerGuild = killerGuild and killerGuild:getId() or 0
            if killerGuild ~= 0 and targetGuild ~= killerGuild and isInWar(playerId, killer:getId()) 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.asyncQuery("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

        local skullTime = killer:getSkullTime()
        if skullTime > 0 then
            local kills = math.ceil(skullTime / configManager.getNumber(configKeys.FRAG_TIME))
            if kills >= killsToBan then
                print("Ban Condition Met")

                -- Verifica se o jogador está na tabela players antes de baní-lo
                local playerName = killer:getName()
                local queryResult = db.storeQuery("SELECT `id` FROM `players` WHERE `name` = " .. db.escapeString(playerName))

                if queryResult ~= false then
                    local playerId = result.getDataInt(queryResult, "id")
                    result.free(queryResult)

                    if playerId ~= false then
                        local timeNow = os.time()
                        db.query("INSERT INTO `account_bans` (`account_id`, `reason`, `banned_at`, `expires_at`, `banned_by`) VALUES (" .. killer:getAccountId() .. ", " .. db.escapeString("Excessive Unjustified Player Killing.") .. ", " .. timeNow .. ", " .. timeNow + (killsBanLenght * 86400) .. ", " .. playerId .. ")")
                        killer:getPosition():sendMagicEffect(CONST_ME_MAGIC_RED)
                        killer:remove()
                    end
                else
                    print("Player not found in the players table.")
                end
            else
                print("Ban Condition Not Met")
            end
        end
    end
end

I fixed my problem. That's my new playerdeath.lua script
 
Last edited:
The query itself is fine. But as Roddet said, the foreign key check failed, there is no player with the id of 1...

Weirdly enough, your new script just sets banned_by to the id of the player who has just been banned. You should just make a fake player with the id of 1 and account id of 0 and call it system :p
 
Back
Top