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

[HELP] Changing spellbook.lua

krithion2424

New Member
Joined
Apr 20, 2008
Messages
73
Reaction score
1
I'm trying to change spellbook.lua to display spells by magic level instead of level. All spells work in spell.xml
I am working with theforgottenserver and heres what I have so far:

function onUse(cid, item, fromPosition, itemEx, toPosition)
local count = getPlayerInstantSpellCount(cid)
local text = ""
local t = {}
for i = 0, count - 1 do
local spell = getPlayerInstantSpellInfo(cid, i)
if spell.maglv ~= 0 then
if spell.manapercent > 0 then
spell.mana = spell.manapercent .. "%"
end
t[#t+1] = spell
end
end
table.sort(t, function(a, b) return a.maglv < b.maglv end)
local prevmaglv = -1
for i, spell in ipairs(t) do
local line = ""
if prevmaglv ~= spell.maglv then
if i ~= 1 then
line = "\n"
end
line = line .. "Spells for Magic Level " .. spell.maglv .. "\n"
prevmaglv = spell.maglv
end
text = text .. line .. " " .. spell.words .. " - " .. spell.name .. " : " .. spell.mana .. "\n"
end
Player(cid):showTextDialog(item.itemid, text)
return true
end

My error in command prompt is:

Lua Script Error: [Action Interface]
data/actions/scripts/other/spellbook.lua:eek:nUse
data/actions/scripts/other/spellbook.lua:14: attempt to compare two nil values stack traceback:
[C]: in funtion '__lt'
data/actions/scripts/other/spellbook.lua:14: in function <data/actions/scripts/other/spellbook.lua:1>
 
Code:
function onUse(cid, item, frompos, item2, topos)
    local count = getPlayerInstantSpellCount(cid)
    local text = "House Spells\n"
    local t = {}

    for i = 0, count - 1 do
        local spell = getPlayerInstantSpellInfo(cid, i)
        if spell.mlevel ~= (nil) then
            if spell.manapercent > 0 then
                spell.mana = spell.manapercent .. "%"
            end

            table.insert(t, spell)
        end
    end
   
    table.sort(t, function(a, b) return a.mlevel < b.mlevel end)

    local prevLevel = -1
    for i, spell in ipairs(t) do
        local line = ""
        if prevLevel ~= spell.mlevel then
            if i ~= 1 then
                line = "\n"
            end

            line = line .. "Spells for Magic level " .. spell.mlevel .. "\n"
            prevLevel = spell.mlevel
        end
       
        text = text .. line .. "  " .. spell.words .. " - " .. spell.name .. " : " .. spell.mana .. "\n"
    end

    doShowTextDialog(cid, item.itemid, text)
    return TRUE
end
 
It worked you both are amazing! Only issue is house spells are combine with magic level 0 spells. Possibly because they both don't require a magic level? How do I have it so they are separate again? like:
House spells
alana sio
aleta sio
___
___
Magic level 0
light healing
light
 
Back
Top