• 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 TFS 1.2 Math.random receives nil value

Ramirow

Veteran OT User
Joined
Aug 22, 2009
Messages
584
Solutions
15
Reaction score
301
Location
Argentina
YouTube
ramirogrant
Guys, im running into an issue, I will leave an example below, and the error code I get, I can't seem to work out WHY it's receiving a nil value, if I print the value of that var before the math.random() function everything looks good, its for sure not a Nil value.

Using TFS 1.2

Code:
local crystals = {
    [18420] = {                    -- Red Crystal config table
    itemid = 18420,
    itemCraftWood1NormalAmount = 7,        --Loop items for normal craft
    itemCraftWood1BonusAmount = 4,        --Loop items for bonus craft
    level = {min = 20, success = 15},
    materials = {wood = 3873, steel = 5887},
    materialRemovalWood = 5,
    materialRemovalSteel = 5,
    skillSuccessIncrement = 1,
    refine = 1,
    xp = 3
    }
}

local luckItem = math.floor(math.random(1, crystals[target.itemid].itemCraftWood1BonusAmount))

It returns this:
Code:
Lua Script Error: [Action Interface]
data/actions/scripts/other/smithing.lua:onUse
data/actions/scripts/other/smithing.lua:196: bad argument #2 to 'random' (number expected, got nil)
stack traceback:
        [C]: at 0x7ff7a2212ef0
        [C]: in function 'random'
        data/actions/scripts/other/smithing.lua:196: in function <data/actions/scripts/other/smithing.lua:59>
 
After looking at your script something that might be causing this problem is your script not finding the itemID inside your crystals array.
Try to add an output statement for debugging and make sure your target.itemid is found under crystals.
PS: Im no expert but surrounding the number with brackets [18420] inside your crystals array seems weird.. can you do it with "18420" = {}??
 
The thing is, im accessing that method to access other vars just fine, for example if I set math.random(1,10) the script continues just fine.
Code:
crystals[target.itemid].xp
returns 3
Code:
crystals[target.itemid].refine
returns 1

So it does recognize the target.itemid correctly it seems
 
Try using math.random(crystals[target.itemid].itemCraftWood1BonusAmount). this should return a number [1, itemCraftWood1BonusAmount]
 
Already fixed it, for some reason, if the var name im giving them contains a number, it always returns a Nil value, changed itemCraftWood1BonusAmount to woodOneBonusAmount and it works perfectly fine now.

I appreciate your help spookie.
 
Lua:
local crystal = crystals[target.itemid]
if crystal then
    local luckItem = math.floor(math.random(1, crystal.itemCraftWood1BonusAmount))
end
 
Already fixed it, for some reason, if the var name im giving them contains a number, it always returns a Nil value, changed itemCraftWood1BonusAmount to woodOneBonusAmount and it works perfectly fine now.

I appreciate your help spookie.
Oh that's interesting. And no problem, I havent done lua in the past but trying to understand it is always a good experience. :)
 
Back
Top