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

Need some help with a "attempt to index nil value"

MikeOT

Well-Known Member
Joined
Jan 22, 2017
Messages
232
Solutions
2
Reaction score
86
Ok, so I'm trying to make a woodcutting script work. I had this fixed before, and was so kindly helped by a few members here. That was a year ago, and I haven't touched it since.

I'm now working with a different distro (still tfs 1.2, but a new project). I tried moving these scripts to the new distro, and I can't get it to work for the life of me... and I have no idea why it is giving me a nil value error.


So here is what I've got:

I created a file called "woodcutting" in my actions scripts folder.

Code:
--Locals that can be set before the action is done should be outside the function itself. This way the code doesnt have to redefine them everytime it runs--
local exhaust = Condition(CONDITION_EXHAUST_WEAPON)
exhaust:setParameter(CONDITION_PARAM_TICKS, (2000))
local logsIds = {3083}
local STORAGE_SKILL_LEVEL = 20020
local STORAGE_SKILL_TRY = 20021
function onUse(cid, item, fromPosition, itemEx, toPosition)
    --These locals require the script to be ran so they are here.--
    local player = Player(cid)
    local target = Item(itemEx.uid)
    --Its always good to make sure locals we set are read--
    if not target then return false end
    --Check if the itemid is in the logIds table first so the script wont read much before stopping here (optimization)--
    if not isInArray(logsIds, itemEx.itemid) then return false end
    --Check exhaust right away so the code doesn't read much if the player is exhausted (optimization)--
    if player:getCondition(CONDITION_EXHAUST_WEAPON) then
       return player:sendTextMessage(MESSAGE_STATUS_SMALL, Game.getReturnMessage(RETURNVALUE_YOUAREEXHAUSTED))
    end
    --This will make sure the players storage value is correct so we can read it in the experience table--
    if player:getStorageValue(STORAGE_SKILL_LEVEL) == nil then
        player:setStorageValue(STORAGE_SKILL_LEVEL, 1)
        player:setStorageValue(STORAGE_SKILL_TRY, 0)
    end
    local skillLevel = player:getStorageValue(STORAGE_SKILL_LEVEL)
    local skillTry = player:getStorageValue(STORAGE_SKILL_TRY)
  
  
    local quant = configs.experience[skillLevel][1]  --------(This is the line that is throwing the nil error)
  
  
    if not quant then return false end
    player:addCondition(exhaust)
    player:addItem(3141,1)
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE,"You cut some logs.")
    toPosition:sendMagicEffect(CONST_ME_BLOCKHIT)
    target:remove(1)
    player:say("+25 Woodcutting Exp", TALKTYPE_MONSTER_SAY)
    player:sendTextMessage(MESSAGE_EXPERIENCE,"You received 25 Woodcutting exp by cutting Logs.")
    if itemEx.itemid == 3083 then
        Game.createItem(3084, 1, toPosition):decay()
    end
    if skillLevel >= 1 and skillLevel <= 10 and skillTry >= (quant-25) then
        setPlayerStorageValue(cid,STORAGE_SKILL_TRY,skillTry + 25)
        setPlayerStorageValue(cid,STORAGE_SKILL_LEVEL,skillLevel + 1)
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have advanced from Woodcutting Level ".. skillLevel .." to Woodcutting Level ".. skillLevel + 1 .."")
        player:getPosition():sendMagicEffect(CONST_ME_FIREWORK_YELLOW)
    else
       setPlayerStorageValue(cid,STORAGE_SKILL_TRY,skillTry + 25)
    end
    return true
end

I then registered the woodcutting axe in actions.xml

I then created an exp table in my data/lib folder:

Code:
rateskill = 1

expmax = 13034431

configs = {
    experience ={
        {83},
        {174},
        {276},
        {388},
        {512},
        {650},
        {801},
        {969},
        {1154},
        {1358}
        }
}

and in lib.lua, added:
Code:
dofile('data/lib/experience.lua')

and in global.lua, added:
Code:
dofile('data/lib/experience.lua')

I feel like this should work. When I use the axe on the tree, nothing happens ingame, and my console tells me that it is attempting to index a nil value with my exp table.

Why is this nil? I thought that by doing this:

Code:
  if player:getStorageValue(STORAGE_SKILL_LEVEL) == nil then
        player:setStorageValue(STORAGE_SKILL_LEVEL, 1)
        player:setStorageValue(STORAGE_SKILL_TRY, 0)
    end

That it would eliminate this problem.

Apparently not.

Thank you.
 
Solution
Ok, so I still can't figure this out.

If I comment out all the stuff to do with the exp table, then the script works. (I can use the axe on a tree, the tree disappears and a stump appears in its place, and logs add to my backpack)

Why is it still returning nil?

I even tried adding this to my login.lua
Code:
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()
        player:setStorageValue(20020, 1)     <--------------------------------
        player:setStorageValue(20021, 0)     <--------------------------------
    else
        if...
Ok, so I still can't figure this out.

If I comment out all the stuff to do with the exp table, then the script works. (I can use the axe on a tree, the tree disappears and a stump appears in its place, and logs add to my backpack)

Why is it still returning nil?

I even tried adding this to my login.lua
Code:
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()
        player:setStorageValue(20020, 1)     <--------------------------------
        player:setStorageValue(20021, 0)     <--------------------------------
    else
        if loginStr ~= "" then
            player:sendTextMessage(MESSAGE_STATUS_DEFAULT, loginStr)
        end


Same thing. attempting to index a nil value.



Edit: Ok, so the problem is definitely that for some reason it is not changing the storage value to 1.

I added a print here:

Code:
local skillLevel = player:getStorageValue(STORAGE_SKILL_LEVEL)
    local skillTry = player:getStorageValue(STORAGE_SKILL_TRY)
print(skillLevel)
and it is still returning -1

The above is AFTER:
Code:
if player:getStorageValue(STORAGE_SKILL_LEVEL) == nil then
        player:setStorageValue(STORAGE_SKILL_LEVEL, 1)
        player:setStorageValue(STORAGE_SKILL_TRY, 0)
    end

What am I doing wrong?
 
Last edited:
Ok, so I still can't figure this out.

If I comment out all the stuff to do with the exp table, then the script works. (I can use the axe on a tree, the tree disappears and a stump appears in its place, and logs add to my backpack)

Why is it still returning nil?

I even tried adding this to my login.lua
Code:
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()
        player:setStorageValue(20020, 1)     <--------------------------------
        player:setStorageValue(20021, 0)     <--------------------------------
    else
        if loginStr ~= "" then
            player:sendTextMessage(MESSAGE_STATUS_DEFAULT, loginStr)
        end


Same thing. attempting to index a nil value.



Edit: Ok, so the problem is definitely that for some reason it is not changing the storage value to 1.

I added a print here:

Code:
local skillLevel = player:getStorageValue(STORAGE_SKILL_LEVEL)
    local skillTry = player:getStorageValue(STORAGE_SKILL_TRY)
print(skillLevel)
and it is still returning -1

The above is AFTER:
Code:
if player:getStorageValue(STORAGE_SKILL_LEVEL) == nil then
        player:setStorageValue(STORAGE_SKILL_LEVEL, 1)
        player:setStorageValue(STORAGE_SKILL_TRY, 0)
    end

What am I doing wrong?
I believe storage values are '-1' by default, not nil.

try changing
Lua:
if player:getStorageValue(STORAGE_SKILL_LEVEL) == nil then
Lua:
if player:getStorageValue(STORAGE_SKILL_LEVEL) < 0 then
whenever you want to confirm if a storage value is not currently in use.
 
Solution
Back
Top