TFS 1.X+ Tfs 1.3 attempt to index local 'spell' (a boolean value)

roriscrave

Intermediate OT User
Joined
Dec 7, 2011
Messages
485
Best answers
12
Reaction score
51
Hi, i made this script to get spell info in tfs 1.3. To check a name of spells.
Whats the problem with the script?


Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)

    for index = 0, getPlayerInstantSpellCount(player) do
        local spell = getPlayerInstantSpellInfo(player, index)
        print(spell.name)
    end
    
    return true
end
error:

Code:
Lua Script Error: [Action Interface]
data/actions/scripts/test.lua:onUse
data/actions/scripts/test.lua:5: attempt to index local 'spell' (a boolean value)
stack traceback:
        [C]: in function '__index'
        data/actions/scripts/test.lua:5: in function <data/actions/scripts/test.lua:1>
 

Stigma

Veteran OT User
Joined
Feb 14, 2015
Messages
4,701
Best answers
414
Reaction score
2,264
You're not handling the case where it returns false due to the player being unable to cast the spell it's referring to, you must use if spell then before doing anything with it.
 

Sarah Wesker

ค∂vαηcε รүηтαx ❤
Joined
Mar 16, 2017
Messages
431
Best answers
39
Reaction score
253
Location
London
this is how it works in spellbooks:
Code:
local count = getPlayerInstantSpellCount(player)
    for i = 0, count - 1 do
        local spell = getPlayerInstantSpellInfo(player, i)
        print(spell.name)
    end
 
OP
R

roriscrave

Intermediate OT User
Joined
Dec 7, 2011
Messages
485
Best answers
12
Reaction score
51
You're not handling the case where it returns false due to the player being unable to cast the spell it's referring to, you must use if spell then before doing anything with it.
this is how it works in spellbooks:
Code:
local count = getPlayerInstantSpellCount(player)
    for i = 0, count - 1 do
        local spell = getPlayerInstantSpellInfo(player, i)
        print(spell.name)
    end
@Sarah Wesker , the same error in console
@Stigma tested with GM and with a normal Player too. (dont print anything in any case)
Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
local count = getPlayerInstantSpellCount(player)
    for i = 0, count - 1 do
        local spell = getPlayerInstantSpellInfo(player, i)
        if spell then
            print(spell.name)
        end
    end
    return true
end
 

Evil Puncker

I'd rather kiss a rattlesnake
Joined
May 30, 2009
Messages
5,489
Best answers
9
Reaction score
1,616
Location
brazol
this might be useful:

Lua:
function getPlayerInstantSpellInfo(cid, spellId)
    local player = Player(cid)
    if not player then
        return false
    end

    local spell = Spell(spellId)
    if not spell or not player:canCast(spell) then
        return false
    end

    return spell
end
 

Stigma

Veteran OT User
Joined
Feb 14, 2015
Messages
4,701
Best answers
414
Reaction score
2,264
Lua:
for _, spell in ipairs(player:getInstantSpells()) do
    print(spell.name)
end
 
Top