• 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 Skill scroll based on used weapon

bybbzan

mapper
Joined
Aug 4, 2012
Messages
809
Solutions
2
Reaction score
135
Location
Sweden
Yo!

I request a skill scroll based on the weapon you are using.
For example a knight using an axe, should get axe fighting if he uses the scroll.
However, if he change to a sword, the scroll should add sword fighting.
Is this possible for 0.4?

Thanks in advance.
 
Try this one, If WEAPON_DISTANCE didn't work properly then replace it with WEAPON_AMMO
Lua:
local config = {maxlvl = 250, trys = 50}

function onUse(cid, item, fromPosition, itemEx, toPosition)
    if getPlayerLevel(cid) <= config.maxlvl and getPlayerWeaponType(cid) == WEAPON_SWORD then
        doPlayerAddSkillTry(cid, 2, config.trys)
        doRemoveItem(item.uid, 1)
          doPlayerSendTextMessage(cid,TALKTYPE_BROADCAST, "CONGRATULATIONS! You gained ".. config.trys .." SKILL TRYS from the SKILL SCROLL!.")
        elseif getPlayerLevel(cid) <= config.maxlvl and getPlayerWeaponType(cid) == WEAPON_AXE then
        doPlayerAddSkillTry(cid, 3, config.trys)
        doRemoveItem(item.uid, 1)
          doPlayerSendTextMessage(cid,TALKTYPE_BROADCAST, "CONGRATULATIONS! You gained ".. config.trys .." SKILL TRYS from the SKILL SCROLL!.")
        elseif getPlayerLevel(cid) <= config.maxlvl and getPlayerWeaponType(cid) == WEAPON_CLUB then
        doPlayerAddSkillTry(cid, 1, config.trys)
        doRemoveItem(item.uid, 1)
          doPlayerSendTextMessage(cid,TALKTYPE_BROADCAST, "CONGRATULATIONS! You gained ".. config.trys .." SKILL TRYS from the SKILL SCROLL!.")
        elseif getPlayerLevel(cid) <= config.maxlvl and getPlayerWeaponType(cid) == WEAPON_DISTANCE then
        doPlayerAddSkillTry(cid, 4, config.trys)
        doRemoveItem(item.uid, 1)
          doPlayerSendTextMessage(cid,TALKTYPE_BROADCAST, "CONGRATULATIONS! You gained ".. config.trys .." SKILL TRYS from the SKILL SCROLL!.")
        elseif getPlayerLevel(cid) >= config.maxlvl then
        doPlayerSendCancel(cid, "Your level is too high.")
        end
  return TRUE
end
You'll have to add this to your globa.lua
Lua:
function getPlayerWeaponType(cid)
    return getItemWeaponType(getPlayerWeapon(cid, true).uid)
end
 
Last edited:
Try this one, If WEAPON_DISTANCE didn't work properly then replace it with WEAPON_AMMO
Lua:
local config = {maxlvl = 250, trys = 50}

function onUse(cid, item, fromPosition, itemEx, toPosition)
    if getPlayerLevel(cid) <= config.maxlvl and getPlayerWeaponType(cid) == WEAPON_SWORD then
        doPlayerAddSkillTry(cid, 2, config.trys)
        doRemoveItem(item.uid, 1)
          doPlayerSendTextMessage(cid,TALKTYPE_BROADCAST, "CONGRATULATIONS! You gained ".. config.trys .." SKILL TRYS from the SKILL SCROLL!.")
        elseif getPlayerLevel(cid) <= config.maxlvl and getPlayerWeaponType(cid) == WEAPON_AXE then
        doPlayerAddSkillTry(cid, 3, config.trys)
        doRemoveItem(item.uid, 1)
          doPlayerSendTextMessage(cid,TALKTYPE_BROADCAST, "CONGRATULATIONS! You gained ".. config.trys .." SKILL TRYS from the SKILL SCROLL!.")
        elseif getPlayerLevel(cid) <= config.maxlvl and getPlayerWeaponType(cid) == WEAPON_CLUB then
        doPlayerAddSkillTry(cid, 1, config.trys)
        doRemoveItem(item.uid, 1)
          doPlayerSendTextMessage(cid,TALKTYPE_BROADCAST, "CONGRATULATIONS! You gained ".. config.trys .." SKILL TRYS from the SKILL SCROLL!.")
        elseif getPlayerLevel(cid) <= config.maxlvl and getPlayerWeaponType(cid) == WEAPON_DISTANCE then
        doPlayerAddSkillTry(cid, 4, config.trys)
        doRemoveItem(item.uid, 1)
          doPlayerSendTextMessage(cid,TALKTYPE_BROADCAST, "CONGRATULATIONS! You gained ".. config.trys .." SKILL TRYS from the SKILL SCROLL!.")
        elseif getPlayerLevel(cid) >= config.maxlvl then
        doPlayerSendCancel(cid, "Your level is too high.")
        end
  return TRUE
end
You'll have to add this to your globa.lua
Lua:
function getPlayerWeaponType(cid)
    local weapon = getPlayerWeapon(cid, true)
    if weapon and weapon.uid > 0 then
        return getItemWeaponType(weapon.uid)
    end
    return WEAPON_NONE
end

I added the function to 050-function since i dont have global.lua in 0.4
The server just crashed. I didnt get any errors what so ever. Everything just freezes.
 
Edited it, Copy the new one instead of the one you added to 050-function and retry.
 
When exactly does it crash? On server start up immediately?
Nvm, it might actually work now, amazing!
It doesnt add 100% tho, just a few percent. Could that be modifed?
19:06 CONGRATULATIONS! You gained 50 SKILL TRYS from the SKILL SCROLL!.

Dont really know what 50 skill trys means.
 
Try with this one
Lua:
local config = {maxlvl = 250, trys = 50}

function onUse(cid, item, fromPosition, itemEx, toPosition)
function getPlayerWeaponType(cid)
    local weapon = getPlayerWeapon(cid, true)
    if weapon and weapon.uid > 0 then
        return getItemWeaponType(weapon.uid)
    end
    return WEAPON_NONE
end
    if getPlayerLevel(cid) <= config.maxlvl and getPlayerWeaponType(cid) == WEAPON_SWORD then
        doPlayerAddSkill(cid, SKILL_SWORD, 1)
        doRemoveItem(item.uid, 1)
          doPlayerSendTextMessage(cid,TALKTYPE_BROADCAST, "CONGRATULATIONS! You gained ".. config.trys .." SKILL TRYS from the SKILL SCROLL!.")
        elseif getPlayerLevel(cid) <= config.maxlvl and getPlayerWeaponType(cid) == WEAPON_AXE then
        doPlayerAddSkill(cid, SKILL_AXE, 1)
        doRemoveItem(item.uid, 1)
          doPlayerSendTextMessage(cid,TALKTYPE_BROADCAST, "CONGRATULATIONS! You gained ".. config.trys .." SKILL TRYS from the SKILL SCROLL!.")
        elseif getPlayerLevel(cid) <= config.maxlvl and getPlayerWeaponType(cid) == WEAPON_CLUB then
        doPlayerAddSkill(cid, SKILL_CLUB, 1)
        doRemoveItem(item.uid, 1)
          doPlayerSendTextMessage(cid,TALKTYPE_BROADCAST, "CONGRATULATIONS! You gained ".. config.trys .." SKILL TRYS from the SKILL SCROLL!.")
        elseif getPlayerLevel(cid) <= config.maxlvl and getPlayerWeaponType(cid) == WEAPON_DISTANCE then
        doPlayerAddSkill(cid, SKILL_DISTANCE, 1)
        doRemoveItem(item.uid, 1)
          doPlayerSendTextMessage(cid,TALKTYPE_BROADCAST, "CONGRATULATIONS! You gained ".. config.trys .." SKILL TRYS from the SKILL SCROLL!.")
        elseif getPlayerLevel(cid) >= config.maxlvl then
        doPlayerSendCancel(cid, "Your level is too high.")
        end
  return TRUE
end
 
Try with this one
Lua:
local config = {maxlvl = 250, trys = 50}

function onUse(cid, item, fromPosition, itemEx, toPosition)
function getPlayerWeaponType(cid)
    local weapon = getPlayerWeapon(cid, true)
    if weapon and weapon.uid > 0 then
        return getItemWeaponType(weapon.uid)
    end
    return WEAPON_NONE
end
    if getPlayerLevel(cid) <= config.maxlvl and getPlayerWeaponType(cid) == WEAPON_SWORD then
        doPlayerAddSkill(cid, SKILL_SWORD, 1)
        doRemoveItem(item.uid, 1)
          doPlayerSendTextMessage(cid,TALKTYPE_BROADCAST, "CONGRATULATIONS! You gained ".. config.trys .." SKILL TRYS from the SKILL SCROLL!.")
        elseif getPlayerLevel(cid) <= config.maxlvl and getPlayerWeaponType(cid) == WEAPON_AXE then
        doPlayerAddSkill(cid, SKILL_AXE, 1)
        doRemoveItem(item.uid, 1)
          doPlayerSendTextMessage(cid,TALKTYPE_BROADCAST, "CONGRATULATIONS! You gained ".. config.trys .." SKILL TRYS from the SKILL SCROLL!.")
        elseif getPlayerLevel(cid) <= config.maxlvl and getPlayerWeaponType(cid) == WEAPON_CLUB then
        doPlayerAddSkill(cid, SKILL_CLUB, 1)
        doRemoveItem(item.uid, 1)
          doPlayerSendTextMessage(cid,TALKTYPE_BROADCAST, "CONGRATULATIONS! You gained ".. config.trys .." SKILL TRYS from the SKILL SCROLL!.")
        elseif getPlayerLevel(cid) <= config.maxlvl and getPlayerWeaponType(cid) == WEAPON_DISTANCE then
        doPlayerAddSkill(cid, SKILL_DISTANCE, 1)
        doRemoveItem(item.uid, 1)
          doPlayerSendTextMessage(cid,TALKTYPE_BROADCAST, "CONGRATULATIONS! You gained ".. config.trys .." SKILL TRYS from the SKILL SCROLL!.")
        elseif getPlayerLevel(cid) >= config.maxlvl then
        doPlayerSendCancel(cid, "Your level is too high.")
        end
  return TRUE
end
Lua:
[19:25:13.645] [Error - Action Interface]
[19:25:13.647] data/actions/scripts/other/expskill.lua:onUse
[19:25:13.651] Description:
[19:25:13.653] data/actions/scripts/other/expskill.lua:12: attempt to call global 'doPlayerAddSkill' (a nil value)
[19:25:13.658] stack traceback:
[19:25:13.660]  data/actions/scripts/other/expskill.lua:12: in function <data/actions/scripts/other/expskill.lua:3>
 
Lua:
[19:25:13.645] [Error - Action Interface]
[19:25:13.647] data/actions/scripts/other/expskill.lua:onUse
[19:25:13.651] Description:
[19:25:13.653] data/actions/scripts/other/expskill.lua:12: attempt to call global 'doPlayerAddSkill' (a nil value)
[19:25:13.658] stack traceback:
[19:25:13.660]  data/actions/scripts/other/expskill.lua:12: in function <data/actions/scripts/other/expskill.lua:3>
add this is data/lib/050-function.lua
Lua:
function doPlayerAddSkill(cid, skill, amount, round)
    if(skill == SKILL__LEVEL) then
        return doPlayerAddLevel(cid, amount, round)
    elseif(skill == SKILL__MAGLEVEL) then
        return doPlayerAddMagLevel(cid, amount)
    end

    return doPlayerAddSkillTry(cid, skill, (getPlayerRequiredSkillTries(cid, skill, getPlayerSkillLevel(cid, skill) + 1) - getPlayerSkillTries(cid, skill)) / getConfigInfo('rateSkill'))
end
 
add this is data/lib/050-function.lua
Lua:
function doPlayerAddSkill(cid, skill, amount, round)
    if(skill == SKILL__LEVEL) then
        return doPlayerAddLevel(cid, amount, round)
    elseif(skill == SKILL__MAGLEVEL) then
        return doPlayerAddMagLevel(cid, amount)
    end

    return doPlayerAddSkillTry(cid, skill, (getPlayerRequiredSkillTries(cid, skill, getPlayerSkillLevel(cid, skill) + 1) - getPlayerSkillTries(cid, skill)) / getConfigInfo('rateSkill'))
end

The skill got stuck at 22, regardless how many times i used the scroll, doesnt move.
 
Try adding this function instead of the one you have
Lua:
function doPlayerAddSkill(cid, skill, amount, round)
   local amount = amount or 1
   if(skill == SKILL__LEVEL) then
     return doPlayerAddLevel(cid, amount, round)
   elseif(skill == SKILL__MAGLEVEL) then
     return doPlayerAddMagLevel(cid, amount)
   end
   for i = 1, amount do
     local currentSkill = getPlayerSkillLevel(cid, skill)
     local nextSkill = currentSkill+1
     local requiredTries = getPlayerRequiredSkillTries(cid, skill, nextSkill)
     local currentTries = getPlayerSkillTries(cid, skill)
     local neededTries = requiredTries - currentTries
     doPlayerAddSkillTry(cid, skill, neededTries, false)
     if getPlayerSkillLevel(cid, skill) == nextSkill then
     end
   end
   return true
end
 
Gonna post this as well, since I think he's trying to raise magic level.
 
Try adding this function instead of the one you have
Lua:
function doPlayerAddSkill(cid, skill, amount, round)
   local amount = amount or 1
   if(skill == SKILL__LEVEL) then
     return doPlayerAddLevel(cid, amount, round)
   elseif(skill == SKILL__MAGLEVEL) then
     return doPlayerAddMagLevel(cid, amount)
   end
   for i = 1, amount do
     local currentSkill = getPlayerSkillLevel(cid, skill)
     local nextSkill = currentSkill+1
     local requiredTries = getPlayerRequiredSkillTries(cid, skill, nextSkill)
     local currentTries = getPlayerSkillTries(cid, skill)
     local neededTries = requiredTries - currentTries
     doPlayerAddSkillTry(cid, skill, neededTries, false)
     if getPlayerSkillLevel(cid, skill) == nextSkill then
     end
   end
   return true
end

It's still not working properly.
If i have sword fighting like this
sword.PNG
And uses the scroll, it gets like this.
sword1.PNG

But when its on 16, and 100% to 17. It works like it should.
 
but that is exactly what the doPlayerAddSkill is supposed to do 😅 you should've explained that you want the scroll to give 100% of skill tries and not the required to go to the next lvl
 
but that is exactly what the doPlayerAddSkill is supposed to do 😅 you should've explained that you want the scroll to give 100% of skill tries and not the required to go to the next lvl

Oh really? Well then im sorry, i thought it was obvious that i want it to add 100% :D
 
Oh really? Well then im sorry, i thought it was obvious that i want it to add 100% :D
Here's my super unelegant math to get this done.
Lua:
local cur_skill = getPlayerSkillLevel(cid, skillid)
local cur_skill_tries = getPlayerSkillTries(cid, skillid)
local skill_tries = getPlayerRequiredSkillTries(cid, skillId, cur_skill)
local skill_tries_next = getPlayerRequiredSkillTries(cid, skillId, cur_skill + 1)
local skill_tries_next_next = getPlayerRequiredSkillTries(cid, skillId, cur_skill + 2)

local current_percent = (cur_skill_tries - skill_tries) / ((skill_tries_next - skill_tries) / 100) -- calculates current percentage to next skill level.
local next_skill_tries_to_percent = ((skill_tries_next_next - skill_tries_next) / 100) * current_percent -- calculates how many skill tries to give to player to reach same percentage in next skill level
local cur_skill_tries_to_next_skill_tries = skill_tries_next - cur_skill_tries -- calculates how many skill tries to give to player to reach next skill level.

local skill_tries_to_give = math.floor(cur_skill_tries_to_next_skill_tries + next_skill_tries_to_percent) -- calculates total amount of skill tries to give to player
doPlayerAddSkillTry(cid, skillid, skill_tries_to_give) -- adds skill tries.
so for example here's what it looks like
Lua:
local cur_skill = 63
local cur_skill_tries = 5600
local skill_tries = 5000
local skill_tries_next = 6100
local skill_tries_next_next = 7300

local current_percent = (5600 - 5000) / ((6100 - 5000) / 100) --> (600) / ((1100) / 100) --> 600 / 11 --> 54.5454
local next_skill_tries_to_percent = ((7300 - 6100) / 100) * 54.5454 --> ((1200) / 100) * 54.5454 --> 12 * 54.5454 --> 654.5448
local cur_skill_tries_to_next_skill_tries = 6100 - 5600 --> 500

local skill_tries_to_give = math.floor(500 + 654.5448) --> math.floor(1154.5448) --> 1154

doPlayerAddSkillTry(cid, skillid, 1154)
 
Last edited:
Here's my super unelegant math to get this done.
Lua:
local cur_skill = getPlayerSkillLevel(cid, skillid)
local cur_skill_tries = getPlayerSkillTries(cid, skillid)
local skill_tries = getPlayerRequiredSkillTries(cid, skillId, cur_skill)
local skill_tries_next = getPlayerRequiredSkillTries(cid, skillId, cur_skill + 1)
local skill_tries_next_next = getPlayerRequiredSkillTries(cid, skillId, cur_skill + 2)

local current_percent = (cur_skill_tries - skill_tries) / ((skill_tries_next - skill_tries) / 100) -- calculates current percentage to next skill level.
local next_skill_tries_to_percent = ((skill_tries_next_next - skill_tries_next) / 100) * current_percent -- calculates how many skill tries to give to player to reach same percentage in next skill level
local cur_skill_tries_to_next_skill_tries = skill_tries_next - cur_skill_tries -- calculates how many skill tries to give to player to reach next skill level.

local skill_tries_to_give = math.floor(cur_skill_tries_to_next_skill_tries + next_skill_tries_to_percent) -- calculates total amount of skill tries to give to player
doPlayerAddSkillTry(cid, skillid, skill_tries_to_give) -- adds skill tries.
so for example here's what it looks like
Lua:
local cur_skill = 63
local cur_skill_tries = 5600
local skill_tries = 5000
local skill_tries_next = 6100
local skill_tries_next_next = 7300

local current_percent = (5600 - 5000) / ((6100 - 5000) / 100) --> (600) / ((1100) / 100) --> 600 / 11 --> 54.5454
local next_skill_tries_to_percent = ((7300 - 6100) / 100) * 54.5454 --> ((1200) / 100) * 54.5454 --> 12 * 54.5454 --> 654.5448
local cur_skill_tries_to_next_skill_tries = 6100 - 5600 --> 500

local skill_tries_to_give = math.floor(500 + 654.5448) --> math.floor(1154.5448) --> 1154

doPlayerAddSkillTry(cid, skillid, 1154)

Nice! so.. how would the script itself look like? 😅
 
Nice! so.. how would the script itself look like? 😅
Try this, I guess.
Lua:
local skill_levels_to_give = 1

local function addSkillTriesByPercent(cid, skillid, amount)
    amount = math.floor(amount)
    if amount < 1 then
        return false
    end
    for i = 1, amount do
        local cur_skill = getPlayerSkillLevel(cid, skillid)
        local cur_skill_tries = getPlayerSkillTries(cid, skillid)
        local skill_tries = getPlayerRequiredSkillTries(cid, skillId, cur_skill)
        local skill_tries_next = getPlayerRequiredSkillTries(cid, skillId, cur_skill + 1)
        local skill_tries_next_next = getPlayerRequiredSkillTries(cid, skillId, cur_skill + 2)
     
        local current_percent = (cur_skill_tries - skill_tries) / ((skill_tries_next - skill_tries) / 100)
        local next_skill_tries_to_percent = ((skill_tries_next_next - skill_tries_next) / 100) * current_percent
        local cur_skill_tries_to_next_skill_tries = skill_tries_next - cur_skill_tries
     
        local skill_tries_to_give = math.floor(cur_skill_tries_to_next_skill_tries + next_skill_tries_to_percent)
        doPlayerAddSkillTry(cid, skillid, skill_tries_to_give)
    end
    return true
end

local function getPlayerWeaponType(cid)
    local weapon = getPlayerWeapon(cid, true)
    if weapon and weapon.uid > 0 then
        return getItemWeaponType(weapon.uid)
    end
    return WEAPON_NONE
end

local function getPlayerSkillByWeaponType(weaponType)
    local skill = WEAPON_NONE
    if weaponType == WEAPON_SWORD then
        skill = SKILL_SWORD
    elseif weaponType == WEAPON_AXE then
        skill = SKILL_AXE
    elseif weaponType == WEAPON_CLUB then
        skill = SKILL_CLUB
    elseif weaponType == WEAPON_DISTANCE then
        skill = SKILL_DISTANCE
    end
    return skill
end

function onUse(cid, item, fromPosition, itemEx, toPosition)
    local skill = getPlayerSkillByWeaponType(getPlayerWeaponType(cid))
    if skill == WEAPON_NONE then
        doPlayerSendCancel(cid, "You need to have a weapon equipped.")
        return true
    end
 
    if addSkillTriesByPercent(cid, skill, skill_levels_to_give) == false then
        doPlayerSendCancel(cid, "An error has occurred. Contact administrator. Error: Xikini-404") -- skill_levels_to_give needs to be a number higher then 0.
        return true
    end

    doPlayerSendTextMessage(cid, TALKTYPE_BROADCAST, "CONGRATULATIONS! You gained ".. skill_levels_to_give .." SKILLS from the SKILL SCROLL!.")
    doRemoveItem(item.uid, 1)
    return true
end
 
Back
Top