crismar Posted April 15, 2014 Share Posted April 15, 2014 Howdy, I have this function (trying to make a crafting system for MTA DaYz from scratch) and I want to check if the player has the necessary materials. The problem is: initially, getElementData returned 'false' (boolean) if the player didn't have the item. Now, I used 'tonumber' and it returns 'nil'. Is it possible to convert 'nil' or 'false' to a integer value (0) ? function craftCheck(thePlayer) local craftPickX, craftPickY = guiGridListGetSelectedItem(craftItems) if craftPickX == 0 then guiSetEnabled(craftGo, false) guiSetText(craftReq, "- Lee Enfield [x1]\n- Empty Soda Cans [x3]") guiSetText(craftName, "Item: CZ-550") guiSetText(craftQ, "Cantitate: 1") if tonumber(getElementData(thePlayer, "Lee Enfield")) >= 1 and tonumber(getElementData(thePlayer, "Empty Soda Cans")) >= 3 then guiSetEnabled(craftGo, true) end end end Debugscript keeps returning this error: attempt to compare number with nil On this line: if tonumber(getElementData(thePlayer, "Lee Enfield")) >= 1 and tonumber(getElementData(thePlayer, "Empty Soda Cans")) >= 3 then Ideas ? Link to comment
JR10 Posted April 15, 2014 Share Posted April 15, 2014 You can use 'or' to assign a different value if it returns false/nil. This will return either a number or 0 tonumber(getElementData(thePlayer, "Lee Enfield")) or 0 Link to comment
crismar Posted April 15, 2014 Author Share Posted April 15, 2014 You can use 'or' to assign a different value if it returns false/nil.This will return either a number or 0 tonumber(getElementData(thePlayer, "Lee Enfield")) or 0 Cheers man Link to comment
Recommended Posts