• 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.4] Level loss system/store level by .txt document

Joriku

Working in the mines, need something?
Joined
Jul 16, 2016
Messages
1,085
Solutions
15
Reaction score
379
Location
Sweden
YouTube
Joriku
Hi,
awhile ago i had some help from one of the best scripters on here aka. @Xikini to help me out with some high-level issues. Those issues we're when I died on a too high level character which couldn't be solved due to it's limitations.
So the genius came up with the idea to store it inside a .txt folder for each player's experience which will allow unlimited integer "int" support which therefore solved my death issues causing either crashes or non death penalty.
The script removes the following levels x from the levels between:
Lua:
local levels = {
    [{  1, 200}] = 1, -- Level 1 to 200, on death. Remove 1 level.
    [{201, 300}] = 2,
    [{301, 400}] = 3,
    [{401, 500}] = 4,
    [{501, 999}] = 5
}

For OTBR users,
This script requires the following functions:
player.lua
Lua:
function Player.addLevel(self, amount, round)
    local experience, level, amount = 0, self:getLevel(), amount or 1
    if amount > 0 then
        experience = getExperienceForLevel(level + amount) - (round and self:getExperience() or getExperienceForLevel(level))
    else
        experience = (round and self:getExperience() or getExperienceForLevel(level)) - getExperienceForLevel(level + amount)
        return self:removeExperience(experience)
    end
    return self:addExperience(experience)
end

global.lua
Lua:
function getExperienceForLevel(level)
    return math.floor((((level - 6) * level + 17) * level - 12) / 6) * 100
end

The script itself is attached onto the thread, enjoy and thanks Xikini for the help!
I take no credits for this, I only asked for perms to post this up.

Lua:
local levels = {
    [{  1, 200}] = 1,
    [{201, 300}] = 2,
    [{301, 400}] = 3,
    [{401, 500}] = 4,
    [{501, 999}] = 5
}

local function writeInfomationToFile(playerName, text)
    local file = io.open("data/logs/player_levels/" .. playerName .. "_currentExperience.txt", "w+")
    file:write(text)
    file:close()
end

local function readInfomationFromFile(playerName)
    local file = io.open("data/logs/player_levels/" .. playerName .. "_currentExperience.txt", "r")
    local text = file:read()
    local number = tonumber(text)
    file:close()
    return number
end

local function adjustExperienceAfterDeath(playerId)
    local player = Player(playerId)
    if not player then
        return
    end
    
    -- reset experience after death
    local playerName = player:getName()
    local experienceToAdd = readInfomationFromFile(playerName) - player:getExperience()
    player:addExperience(experienceToAdd)
    writeInfomationToFile(playerName, "-1")
    
    -- remove levels
    local playerLevel = player:getLevel()
    for v, k in pairs(levels) do
        if v[2] >= playerLevel and v[1] <= playerLevel then
            print("Removing " .. k .. " levels from " .. playerName)
            for i = 1, k do
                player:addLevel(-1, false)
            end
            break
        end
    end
    return true
end

local onDeath_creatureEvent = CreatureEvent("onDeath_levelLossSystem")

function onDeath_creatureEvent.onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
    local playerName = creature:getName()
    writeInfomationToFile(playerName, "" .. creature:getExperience() .. "")
    return true
end
        
onDeath_creatureEvent:register()

local onLogin_creatureEvent = CreatureEvent("onLogin_RegisterEvents2")

function onLogin_creatureEvent.onLogin(player)
    local storageValue = -1
    local playerName = player:getName()
    local file = io.open("data/logs/player_levels/" .. playerName .. "_currentExperience.txt", "r")
    if file ~= nil then
        file:close()
        storageValue = readInfomationFromFile(playerName)
    else
        writeInfomationToFile(playerName, "-1")
    end
    player:registerEvent("onDeath_levelLossSystem")
    if storageValue > 0 then
        addEvent(adjustExperienceAfterDeath, 100, player:getId())
    end
    return true
end

onLogin_creatureEvent:register()
 

Attachments

  • level_loss_system.lua
    2.2 KB · Views: 24 · VirusTotal
Last edited by a moderator:
This is awesome! Anyway to make it save this information for each vocation? I tried to mess around with the script for a while but have no idea where to start. Can you point me in the right direction?

Thanks for sharing and shout out to Xikini o/
 
Back
Top