• 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 How to store a function inside a variable for future purpose? It returns nil. :(

waqmaz

Member
Joined
Jun 17, 2015
Messages
203
Reaction score
11
How to store a function inside a variable for future purpose? It returns nil. :(

Code:
attackBefore = getItemAttribute(item.uid, 'attack')
attackAfter = 1
doItemSetAttribute(item.uid, 'attack', attackAfter)
print('Attack changed from '..attackBefore..' to '..attackAfter)

I gives:
attempt to concatenate global 'attackBefore' (a nil value)
 
How to store a function inside a variable for future purpose? It returns nil. :(

Code:
attackBefore = getItemAttribute(item.uid, 'attack')
attackAfter = 1
doItemSetAttribute(item.uid, 'attack', attackAfter)
print('Attack changed from '..attackBefore..' to '..attackAfter)

I gives:
There is nothing wrong with your code, the problem is there are not default values for attributes, they are only set when you use doItemSetAttribute so if you try to get the attribute that was not previously assigned you will get a nil returned and it will give you this error when you try to concatenate with a string without doing any check.

What you can do:
Code:
attackBefore = getItemAttribute(item.uid, 'attack') or getItemInfo(item.itemid).attack
attackAfter = 1
if attackBefore then
    doItemSetAttribute(item.uid, 'attack', attackAfter)
    print('Attack changed from '..attackBefore..' to '..attackAfter)
end
 
Men, I've lost my mind again...
It keeps saying:
[20/08/2016 18:19:14] error 1
[20/08/2016 18:19:14] error 2
[20/08/2016 18:19:14] error 3
Why?
Code:
slotLeft = getPlayerSlotItem(cid, CONST_SLOT_LEFT)
   
     if msgcontains(msg, 'white') then
       
       attackBefore = getItemAttribute(slotLeft.uid, 'attack')
       
       if attackBefore then
         print('attackBefore')   
       else
         print('error 1')   
       end
       
       if attackBefore ~= nil then
         print('attackBefore ~= nil')   
       else
         print('error 2')   
       end
       
       if attackBefore == -1 then
         print('attackBefore == -1')   
       else
         print('error 3')   
       end
       
     end
 
Last edited:
Back
Top