• 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 Intentialy reset level on death

fishie

fishies-war
Joined
Oct 27, 2016
Messages
78
Reaction score
2
Hello, ive been trying to get this to work for a while with no success. Ive created a custom war server and i need it so when a player dies his level resets back to basic (level 100) but nothing ive tried has worked.

I had a script created for me by xeraphus but sadly this only worked under basic conditions and eventurely didnt work at all.

local function getExperienceForLevel(lv)
lv = lv - 1
return ((50 * lv * lv * lv) - (150 * lv * lv) + (400 * lv)) / 3
end

deathStorage = 'EXPERIENCE'

function onPrepareDeath(creature, killer)
local player = creature:getPlayer()
if not player then
return true
end
player:setStorageValue(deathStorage, getExperienceForLevel(100) - player:getExperience())
return true
end

Also this i added to login.lua

function onLogin(player)
local exp = player:getStorageValue(deathStorage)
if exp ~= 0 then
if exp < 0 then
player:removeExperience(exp)
else
player:addExperience(exp)
end
player:setStorageValue(deathStorage, 0)
player:sendTextMessage(MESSAGE_INFO_DESCR, 'Your level has been reset.')
end
Can anyone make a new way of reseting a players level on death? Im using TFS 1.2

Thankyou. Anymore details ive missed please ask
 
Last edited:
Solution
Done it twice because I wasn't sure what you meant. First one I copied it into the script and it didn't work with no errors at all, second one which gave the error was because I made two scripts. :/
my bad i misspelled
Code:
function onPrepareDeath(creature, killer)
    local player = creature:getPlayer()
    if not player then
        return true
    end
    player:setStorageValue('death', 1)
    return true
end

Code:
local function getExpForLevel(lv)
    lv = lv - 1
    return ((50 * lv * lv * lv) - (150 * lv * lv) + (400 * lv)) / 3
end

function onLogin(player)
    local resetExperience = getExpForLevel(100)
    local currentExperience = player:getExperience()
    if player:getStorageValue('death') == 1 then
        if...
I would just set a storage to the player when he dies. Then make a global event that checks the storage and sets a players level, ect. (possible solution)
 
Haven't tried either I believe but im quite a noob when it comes to this stuff so wouldn't know how
You can do something like this:
Code:
local function getExpForLevel(level)
    level = level - 1
    return ((50 * level * level * level) - (150 * level * level) + (400 * level)) / 3
end

function Player:onLoseExperience(exp)
    if self:getLevel() >= 100 then
        local currentExp = self:getExperience()
        local minExp = getExpForLevel(100)
        if minExp >= currentExp - exp then
            return currentExp - minExp
        end
    end

    return exp
end
 
did you ever test my script
it gives the player a storage value onPrepareDeath which is used onLogin to make sure that they died, (the natural experience loss will have already taken place), after that it will remove experience from the player if he/she is over level 100, if he/she is under 100 exp will be added to make them 100 just in case they get under 100 somehow

I agree with Xeraphus. This is how I would do it.

When the player dies - Give storage value.
When the player logs in - If player has storage value, reset level and reset storage value
 
There seems to be different formulas? Or my mind is too dull to understand they are the same thing..
Idk I use this and works for me.
In my server its little bit different as I use percentage for leveling, but you should get the idea or someeone else can convert it for you.
Code:
local function getTotalExp(level)
local nextExp = (50*level*level*level/3) - (100*level*level) + (850*level/3) - 200
    if nextExp > 0 then return nextExp end
    return 0
end


function Player:addExpPercent(percent)
local player = self
local L = player:getLevel()
local totalExp = getTotalExp(L+1)
local expNeeded = totalExp - getTotalExp(L)
local playerExp = player:getExperience()
local missingExp = math.ceil(totalExp - playerExp)
local addExp = math.floor(expNeeded*percent*0.01)

    if addExp > missingExp then
        local nextTotalExp = getTotalExp(L+2)
        local newExpNeeded = nextTotalExp - getTotalExp(L+1)
        local usedPercent = math.ceil(100/(expNeeded/missingExp))
        local extraExp = math.floor(newExpNeeded * (percent - usedPercent)*0.01)
     
        addExp = missingExp + extraExp
    end
 
    if addExp > 0 then
        player:addExperience(addExp)
        player:sendTextMessage(ORANGE, "You earned: "..percent.."% experience.")
    else
        print("addExpPercent wanted to crash!")
    end
end

function Player:removeExpPercent(percent)
local player = self
local L = player:getLevel()
local totalExp = getTotalExp(L+1)
local playerExp = player:getExperience()
local expNeeded = totalExp - getTotalExp(L)
local currentExp = math.floor(expNeeded - (totalExp - playerExp))
    if currentExp <= 0 then currentExp = 0 end
local removeExp = math.floor(expNeeded*percent*0.01)

    if removeExp > currentExp then
        local nextTotalExp = getTotalExp(L)
        local newExpNeeded = nextTotalExp - getTotalExp(L-1)
        local usedPercent = percent - math.floor(100/removeExp/currentExp)
            if currentExp == 0 then usedPercent = 0 end
        local extraExp = math.floor(newExpNeeded * (percent - usedPercent)*0.01)
        removeExp = currentExp + extraExp
    end
 
    if removeExp > 0 then
        player:removeExperience(removeExp)
        player:sendTextMessage(ORANGE, "You lost: "..percent.."% experience.")
    else
        print("removeExpPercent wanted to crash!")
    end
end

You can also be lazy and us same code by removing all exp on login and then adding 10000% exp
 
I agree with Xeraphus. This is how I would do it.

When the player dies - Give storage value.
When the player logs in - If player has storage value, reset level and reset storage value

I'm happy to give that a go but the script he posted didn't work. I'll test it again and all other methods posted again shortly when I'm home and let you know results.
 
I'm happy to give that a go but the script he posted didn't work. I'll test it again and all other methods posted again shortly when I'm home and let you know results.

Can you explain what doesn't work about it?

It is a very simple script, and should work 100% of the time.
 
Tried Xeraphus script one more time and am now getting this error. pretty sure ive done everything right.

Dec 15 17:54:02 158-69-12-209 tfs[28926]: >> Loaded all modules, server starting up...
Dec 15 17:54:02 158-69-12-209 tfs[28926]: >> Forgotten Server Online!
Dec 15 17:54:07 158-69-12-209 tfs[28926]: Admin Fish has logged in.
Dec 15 17:54:07 158-69-12-209 tfs[28926]: Lua Script Error: [CreatureScript Interface]
Dec 15 17:54:07 158-69-12-209 tfs[28926]: data/creaturescripts/scripts/loginreset.lua:eek:nLogin
Dec 15 17:54:07 158-69-12-209 tfs[28926]: data/creaturescripts/scripts/loginreset.lua:8: attempt to call global 'getExpForLevel' (a nil value)
Dec 15 17:54:07 158-69-12-209 tfs[28926]: stack traceback:
Dec 15 17:54:07 158-69-12-209 tfs[28926]: [C]: in function 'getExpForLevel'
Dec 15 17:54:07 158-69-12-209 tfs[28926]: data/creaturescripts/scripts/loginreset.lua:8: in function <data/creaturescripts/scripts/loginreset.lua:7>
Dec 15 17:54:07 158-69-12-209 tfs[28926]: Admin Fish has logged out.
 
Tried Xeraphus script one more time and am now getting this error. pretty sure ive done everything right.

Dec 15 17:54:02 158-69-12-209 tfs[28926]: >> Loaded all modules, server starting up...
Dec 15 17:54:02 158-69-12-209 tfs[28926]: >> Forgotten Server Online!
Dec 15 17:54:07 158-69-12-209 tfs[28926]: Admin Fish has logged in.
Dec 15 17:54:07 158-69-12-209 tfs[28926]: Lua Script Error: [CreatureScript Interface]
Dec 15 17:54:07 158-69-12-209 tfs[28926]: data/creaturescripts/scripts/loginreset.lua:eek:nLogin
Dec 15 17:54:07 158-69-12-209 tfs[28926]: data/creaturescripts/scripts/loginreset.lua:8: attempt to call global 'getExpForLevel' (a nil value)
Dec 15 17:54:07 158-69-12-209 tfs[28926]: stack traceback:
Dec 15 17:54:07 158-69-12-209 tfs[28926]: [C]: in function 'getExpForLevel'
Dec 15 17:54:07 158-69-12-209 tfs[28926]: data/creaturescripts/scripts/loginreset.lua:8: in function <data/creaturescripts/scripts/loginreset.lua:7>
Dec 15 17:54:07 158-69-12-209 tfs[28926]: Admin Fish has logged out.
you didn't copy the function into the login file
 
Done it twice because I wasn't sure what you meant. First one I copied it into the script and it didn't work with no errors at all, second one which gave the error was because I made two scripts. :/
 
Done it twice because I wasn't sure what you meant. First one I copied it into the script and it didn't work with no errors at all, second one which gave the error was because I made two scripts. :/
my bad i misspelled
Code:
function onPrepareDeath(creature, killer)
    local player = creature:getPlayer()
    if not player then
        return true
    end
    player:setStorageValue('death', 1)
    return true
end

Code:
local function getExpForLevel(lv)
    lv = lv - 1
    return ((50 * lv * lv * lv) - (150 * lv * lv) + (400 * lv)) / 3
end

function onLogin(player)
    local resetExperience = getExpForLevel(100)
    local currentExperience = player:getExperience()
    if player:getStorageValue('death') == 1 then
        if player:getLevel() > 100 then
            player:removeExperience(currentExperience - resetExperience)
        elseif player:getLevel() < 100 then
            player:addExperience(resetExperience - currentExperience)
        end
        player:setStorageValue('death', -1)
    end
    return true
end

this will work, i've tested it
if player is lower than 100 it will add experience to get them back to level 100
if they are over 100 it will remove experience to level 100
 
Last edited:
Solution
Am i doing something wrong? It still dosnt work for me.

The script,

<event type="preparedeath" name="resetlevel" script="resetlevel.lua" />
function onPrepareDeath(creature, killer)
local player = creature:getPlayer()
if not player then
return true
end
player:setStorageValue('death', 1)
return true
end

my login script.

local events = {'PlayerDeath', 'DropLoot', 'PK Storage', 'resetlevel'}

function onLogin(player)

local function getExpForLevel(lv)
lv = lv - 1
return ((50 * lv * lv * lv) - (150 * lv * lv) + (400 * lv)) / 3
end

function onLogin(player)
local resetExperience = getExpForLevel(100)
local currentExperience = player:getExperience()
if player:getStorageValue('death') == 1 then
if player:getLevel() > 100 then
player:removeExperience(currentExperience - resetExperience)
elseif player:getLevel() < 100 then
player:addExperience(resetExperience - currentExperience)
end
player:setStorageValue('death', -1)
end
end
local loginStr = "Welcome to " .. configManager.getString(configKeys.SERVER_NAME) .. "!"
if player:getLastLoginSaved() <= 0 then
loginStr = loginStr .. " Please choose your outfit."
player:sendOutfitWindow()
else
if loginStr ~= "" then
player:sendTextMessage(MESSAGE_STATUS_DEFAULT, loginStr)
end

loginStr = string.format("Your last visit was on %s.", os.date("%a %b %d %X %Y", player:getLastLoginSaved()))
end
player:sendTextMessage(MESSAGE_STATUS_DEFAULT, loginStr)

-- Stamina
nextUseStaminaTime[player.uid] = 0

-- Promotion
local vocation = player:getVocation()
local promotion = vocation:getPromotion()
if player:isPremium() then
local value = player:getStorageValue(STORAGEVALUE_PROMOTION)
if not promotion and value ~= 1 then
player:setStorageValue(STORAGEVALUE_PROMOTION, 1)
elseif value == 1 then
player:setVocation(promotion)
end
elseif not promotion then
player:setVocation(vocation:getDemotion())
end

-- Events
for _, event in ipairs(events) do
player:registerEvent(event)
end
return true
end
 
i use death and preparedeath dont work
Code:
[Warning - Event::checkScript] Event onDeath not found. scripts/tfws_creaturescripts/reset.lua
[Warning - Event::checkScript] Event onPrepareDeath not found. scripts/tfws_creaturescripts/reset.lua
 
because you put the event names wrong
event="death"
event="preparedeath"


I tried to make it simpler, but it did not work and it did not error
Code:
    <event type="death" name="reset" script="tfws_creaturescripts/reset.lua"/>

Code:
function onDeath(cid)
db.query("UPDATE `players` SET `level` = '8', `health` = '180', `healthmax` = '180', `experience` = '4200', `maglevel` = '0', `mana` = '35', `manamax` = '35', `cap` = '400', `skill_fist` = '10', `skill_club` = '10', `skill_sword` = '10', `skill_axe` = '10', `skill_dist` = '10', `skill_shielding` = '10' WHERE `id` = " .. getPlayerGUID(cid) .. ";")
    return true
end
 
I tried to make it simpler, but it did not work and it did not error
Code:
    <event type="death" name="reset" script="tfws_creaturescripts/reset.lua"/>

Code:
function onDeath(cid)
db.query("UPDATE `players` SET `level` = '8', `health` = '180', `healthmax` = '180', `experience` = '4200', `maglevel` = '0', `mana` = '35', `manamax` = '35', `cap` = '400', `skill_fist` = '10', `skill_club` = '10', `skill_sword` = '10', `skill_axe` = '10', `skill_dist` = '10', `skill_shielding` = '10' WHERE `id` = " .. getPlayerGUID(cid) .. ";")
    return true
end
idk why people take my working code and change it then tell me it doesnt work
just copy the code that i made
it works
pls.
 
Back
Top