简体   繁体   中英

Lua error: string expected, got nil

I need help with my scripts. I tried nearly everything, but I can't figure it out what the problem is. I want look.lua to check if str = str.."\\nIt's "..getPokemonAge(thing.uid).." old." Returns with nil, then ignores it and goes on with the script.

This is the error I get on console:

[04/12/2012 20:43:42] [Error - CreatureScript Interface] 
[04/12/2012 20:43:42] data/creaturescripts/scripts/look.lua:onLook
[04/12/2012 20:43:42] Description: 
[04/12/2012 20:43:42] data/lib/011-string.lua:16: bad argument #1 to 'find' (string expected, got nil)
[04/12/2012 20:43:42] stack traceback:
[04/12/2012 20:43:42]   [C]: in function 'find'
[04/12/2012 20:43:42]   data/lib/011-string.lua:16: in function '(for generator)'
[04/12/2012 20:43:42]   data/lib/011-string.lua:16: in function 'explode'
[04/12/2012 20:43:42]   data/lib/age system.lua:2: in function 'getPokemonYears'
[04/12/2012 20:43:42]   data/lib/age system.lua:42: in function 'getPokemonAge'
[04/12/2012 20:43:42]   data/creaturescripts/scripts/look.lua:32: in function <data/creaturescripts/scripts/look.lua:1>

011-string.lua

local i, pos, tmp, t = 0, 1, "", {}
        for s, e in function() return string.find(str, sep, pos) end do
            tmp = str:sub(pos, s - 1):trim()
            table.insert(t, tmp)
            pos = e + 1

            i = i + 1

        end

look.lua

str = str.."\nIt's "..getPokemonAge(thing.uid).." old."

age system.lua

function getPokemonYears(pokeball)
local data = string.explode(getItemAttribute(pokeball, "pokeballinfo"), "/")
-- data[1] = dia, data[2] = mes, data[3] = ano
local yearnow = math.floor(tonumber(os.date("%Y")))
local monthnow = math.floor(tonumber(os.date("%m")))
local daynow = math.floor(tonumber(os.date("%d")))
local ano = math.floor(tonumber(data[3]))
local mes = math.floor(tonumber(data[2]))
local dia = math.floor(tonumber(data[1]))
local years = 0
if yearnow == ano then years = monthnow-mes end
if yearnow > ano then years = (12-mes) + monthnow end
return years
end

function getPokemonMonths(pokeball)
local data = string.explode(getItemAttribute(pokeball, "pokeballinfo"), "/")
local yearnow = math.floor(tonumber(os.date("%Y")))
local monthnow = math.floor(tonumber(os.date("%m")))
local daynow = math.floor(tonumber(os.date("%d")))
local ano = math.floor(tonumber(data[3]))
local mes = math.floor(tonumber(data[2]))
local dia = math.floor(tonumber(data[1]))

if (yearnow == ano) and (monthnow==mes) and (daynow<dia+2.5) then months = 0 end
if (yearnow == ano) and (monthnow==mes) and (daynow>dia+2.5) then months = (daynow-dia)/2.5 end
if (yearnow == ano) and (monthnow>mes) then months = math.floor((30-dia)/2.5) + daynow/2.5 end
if (yearnow > ano) then
days = math.floor(monthnow*30+daynow)
months = math.floor(days/2.5)
end
if tostring(months):len() > 3 then months2 = tonumber(string.sub(tostring(months), 1, 3))
else months2 = months end
return months
end


function getPokemonAge(pokeball)
return ""..getPokemonYears(pokeball).." year, "..getPokemonMonths(pokeball).." months"
end

I think I finally understand your question, so I will rephrase how I understand it and you can tell whether that was what you wanted.

As I understand it you know that your function getPokemonAge sometimes causes an error. Several others pointed out that this error is from getItemAttribute(pokeball, "pokeballinfo") returning nil .

Now I think you want the program to return the text if a text was produced, but to ignore any error that might occur and return nil in case of error.

This can be done with pcall ( look here ).

In my partial rewrite of your getPokemonAge-Function I call getPokemonAgeInternal (which is your orginal function) with pcall. Then I just check the result and return nil on error.

function getPokemonAgeInternal(pokeball)

    return ""..getPokemonYears(pokeball).." year, "..getPokemonMonths(pokeball).." months"
end

function getPokenmonAge(pokeball)
    success, value = pcall( getPokemonAgeInternal, pokeball )
    if ( success )
    then
        return value
    else
        return nil
    end
end

You can apply similar code to your getPokemonYears -Function instead if you want to protect that against errors.

If your error always comes from getItemAttribute(pokeball, "pokeballinfo") being nil you should not use pcall, but instead just check that condition and return nil if getItemAttribute(pokeball, "pokeballinfo") == nil .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM