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

Solved Levelup reward system (10.98)

monkeyy

Member
Joined
Aug 20, 2011
Messages
153
Reaction score
9
Hey guys!

Im using The Forgotten Server 1.3 and I would like to create a reward system where players receive 2cc at lvl 20 , 5cc at lvl 50 etc. etc.

Currently I have 3 files:
Login.lua
Lua:
function onLogin(player)
    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
    player:registerEvent("PlayerDeath")
    player:registerEvent("DropLoot")
    player:registerEvent("RewardChest")
    registerCreatureEvent(cid, "LevelUp")
    return true
end
levelup.lua
Lua:
local storage = 3746
local levelReward = {
   [1] = {10, 2160, 1, "crystal coin"},
   [2] = {30, 2160, 3, "crystal coins"}, -- lowest level Must be at top, descending down
   [3] = {50, 2160, 5, "crystal coins"}, -- level, itemID, item_amount, item_name
   [4] = {100, 2160, 10, "crystal coins"}
}
function onAdvance(cid, skill, oldLevel, newLevel)
   -- this is to make sure that the players skill is actually increasing
   -- it's also to check that it's their level that is increasing, and not their fisting skill or something
   if skill ~= SKILL__LEVEL or oldlevel >= newLevel then
       return true
   end
   doCreatureAddHealth(cid, getCreatureMaxHealth(cid))
   doCreatureAddMana(cid, getCreatureMaxMana(cid))
   for i = 1, #levelReward do
       local table_level = levelReward[i][1]
       if newLevel >= table_level and table_level > getPlayerStorageValue(cid, storage) then
           local table_count = levelReward[i][3]
           doPlayerAddItem(cid, levelReward[i][2], table_count, true)
           doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Congratulations, system has sent " .. table_count .. " " .. levelReward[i][4] .. " as a reward for level " .. table_level .. " to your location!")
           setPlayerStorageValue(cid, storage, table_level)
           break
       end
   end
   return true
end
And Creaturescripts.xml
Lua:
<?xml version="1.0" encoding="UTF-8"?>
<creaturescripts>
    <event type="login" name="PlayerLogin" script="login.lua" />
    <event type="logout" name="PlayerLogout" script="logout.lua" />
    <event type="login" name="FirstItems" script="firstitems.lua" />
    <event type="login" name="OfflineTraining" script="offlinetraining.lua" />
    <event type="login" name="RegenerateStamina" script="regeneratestamina.lua" />
    <event type="death" name="PlayerDeath" script="playerdeath.lua" />
    <event type="death" name="DropLoot" script="droploot.lua" />
    <event type="extendedopcode" name="ExtendedOpcode" script="extendedopcode.lua" />
    <event type="death" name="WhiteDeerDeath" script="others/whitedeer.lua" />
    <event type="kill" name="RewardChest" script="reward_chest.lua"/>
    <event type="advance" name="LevelUp" event="script" value="levelup.lua"/>
</creaturescripts>



I tried several level.lua scripts but everytime I save>restart server and test i, it doesnt want to work :(

Since I dont get errors in my server, I cant find where the problem lies.

Hope somone can help me out :)
 
Last edited:
Solution
cid was used in older scripts to identify the player, in 1.x we have userdata to do that instead of a unique number (even though the cid is still used sometimes)
pretty much anything on the left of a colon is a userdata (with a few exceptions being a Position and a ModalWindow)
in 1.x OOP (Object Oriented Programming) concepts were introduced, so we don't use cid in functions most of the time since the userdata provides the creature to us instead of an id, which is why the argument here is changed from cid to player in onAdvance(player, skill, oldLevel, newLevel)
the only reason old functions work (such as getStorageValue(cid, storage)) is because it goes through a compat function in compat.lua and uses the newer version of the function...
change this: registerCreatureEvent(cid, "LevelUp")
to this: player:registerEvent("LevelUp")

cid doesn't exist within the scope of onLogin because it was never defined, you were trying to use the old way of registering
 
change this: registerCreatureEvent(cid, "LevelUp")
to this: player:registerEvent("LevelUp")

cid doesn't exist within the scope of onLogin because it was never defined, you were trying to use the old way of registering

I changed it, but it still doesnt give my character the rewards:
Lua:
function onLogin(player)
    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
    player:registerEvent("PlayerDeath")
    player:registerEvent("DropLoot")
    player:registerEvent("RewardChest")
    player:registerEvent("LevelUp")
    return true
end

Should the other files be altered to ?
Could you explain what ''cid'' means? I see it almost in every script.
 
cid was used in older scripts to identify the player, in 1.x we have userdata to do that instead of a unique number (even though the cid is still used sometimes)
pretty much anything on the left of a colon is a userdata (with a few exceptions being a Position and a ModalWindow)
in 1.x OOP (Object Oriented Programming) concepts were introduced, so we don't use cid in functions most of the time since the userdata provides the creature to us instead of an id, which is why the argument here is changed from cid to player in onAdvance(player, skill, oldLevel, newLevel)
the only reason old functions work (such as getStorageValue(cid, storage)) is because it goes through a compat function in compat.lua and uses the newer version of the function instead
to show you what i mean:
Lua:
function getPlayerStorageValue(cid, key) local p = Player(cid) return p ~= nil and p:getStorageValue(key) or false end
it creates a player userdata and uses that userdata to use p:getStorageValue(key), which is the newer version of the function

edit: for more information about OOP in Lua: Programming in Lua : 16

here's the working script (i converted the old functions to the newer ones too)
Lua:
local storage = 3746
local levelReward = {
    [1] = {10, 2160, 1, "crystal coin"},
    [2] = {30, 2160, 3, "crystal coins"}, -- lowest level Must be at top, descending down
    [3] = {50, 2160, 5, "crystal coins"}, -- level, itemID, item_amount, item_name
    [4] = {100, 2160, 10, "crystal coins"}
}

function onAdvance(player, skill, oldLevel, newLevel)
    -- this is to make sure that the players skill is actually increasing
    -- it's also to check that it's their level that is increasing, and not their fisting skill or something
    if skill ~= SKILL_LEVEL then
        return true
    end
    player:addHealth(player:getMaxHealth())
    player:addMana(player:getMaxMana())
    for i = 1, #levelReward do
        local table_level = levelReward[i][1]
        if newLevel >= table_level and table_level > player:getStorageValue(storage) then
            local table_count = levelReward[i][3]
            player:addItem(levelReward[i][2], table_count, true)
            player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Congratulations, system has sent " .. table_count .. " " .. levelReward[i][4] .. " as a reward for level " .. table_level .. " to your location!")
            player:setStorageValue(storage, table_level)
        end
    end
    return true
end
 
Solution
Learned something again :) Thank you for the detailed explanation! But unfortunately it still doesnt work :(

This is how everything is now:
levelup.lua
Lua:
local storage = 3746
local levelReward = {
    [1] = {10, 2160, 1, "crystal coin"},
    [2] = {30, 2160, 3, "crystal coins"}, -- lowest level Must be at top, descending down
    [3] = {50, 2160, 5, "crystal coins"}, -- level, itemID, item_amount, item_name
    [4] = {100, 2160, 10, "crystal coins"}
}
function onAdvance(player, skill, oldLevel, newLevel)
    -- this is to make sure that the players skill is actually increasing
    -- it's also to check that it's their level that is increasing, and not their fisting skill or something
    if skill ~= SKILL_LEVEL then
        return true
    end
    player:addHealth(player:getMaxHealth())
    player:addMana(player:getMaxMana())
    for i = 1, #levelReward do
        local table_level = levelReward[i][1]
        if newLevel >= table_level and table_level > player:getStorageValue(storage) then
            local table_count = levelReward[i][3]
            player:addItem(levelReward[i][2], table_count, true)
            player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Congratulations, system has sent " .. table_count .. " " .. levelReward[i][4] .. " as a reward for level " .. table_level .. " to your location!")
            player:setStorageValue(storage, table_level)
        end
    end
    return true
end
Login.lua
Lua:
function onLogin(player)
    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
    player:registerEvent("PlayerDeath")
    player:registerEvent("DropLoot")
    player:registerEvent("RewardChest")
    player:registerEvent("LevelUp")
    return true
end
And creaturescript.xml
Lua:
<event type="advance" name="LevelUp" event="script" value="levelup.lua"/>
 
Back
Top