Lua Problem attempt to perform arithmetic on a string value

Joined
Apr 26, 2008
Messages
349
Best answers
0
Likes
5
#1
Hi, can anyone help me with this?

[Error - CreatureScript Interface]
data/creaturescripts/scripts/rewardchest_boss.lua:eek:nStatsChange
Description:
data/creaturescripts/scripts/rewardchest_boss.lua:104: attempt to perform arithmetic on a string value
stack traceback:
data/creaturescripts/scripts/rewardchest_boss.lua:104: in function <data/creaturescripts/scripts/rewardchest_boss.lua:100>

Code:
dofile('data/sistemas/rewardchest.lua')

local function addRewardLoot(uid, bossName, tabela_reward)
    local money = math.random(10, 40)
    local msg = "The following items are available in your reward chest:"
    local chest = doCreateItemEx(REWARDCHEST.rewardBagId)

doItemSetAttribute(chest, "description", "Reward System has kill the boss ".. bossName ..".")

    if table.maxn(tabela_reward) > 0 then
        for x = 1, table.maxn(tabela_reward) do
            local rand = math.random(100)
            if rand <= tabela_reward[x][3] then
                local count = math.random(1, tabela_reward[x][2])
                doAddContainerItem(chest, tabela_reward[x][1], count)
msg = msg .. " ".. (tabela_reward[x][2] > 1 and tabela_reward[x][2] or "") .." ".. getItemNameById(tabela_reward[x][1]) ..","
            end
        end
        doPlayerSendTextMessage(uid, MESSAGE_INFO_DESCR, msg .. " and ".. money .." platinum coins.")
    else
        doPlayerSendTextMessage(uid, MESSAGE_INFO_DESCR, msg .. " ".. money .." platinum coins.")
    end

    doAddContainerItem(chest, 2152, money)
    doPlayerSendMailByName(getPlayerName(uid), chest, REWARDCHEST.town_id)

    local boss = REWARDCHEST.bosses[bossName]
    setPlayerStorageValue(uid, boss.storage, 0)
    doSendMagicEffect(getPlayerPosition(uid), CONST_ME_MAGIC_BLUE)
end

local function addLoot(tabela_loot, tabela_reward, all_loot)
    if table.maxn(tabela_loot) > 0 then
        if all_loot then
            for x = 1, table.maxn(tabela_loot) do
                table.insert(tabela_reward, tabela_loot[x])
            end
        else
            table.insert(tabela_reward, tabela_loot[math.random(table.maxn(tabela_loot))])
        end
    end

    return tabela_reward
end

local function rewardChestSystem(bossName)
    local players = {}
    local boss = REWARDCHEST.bosses[bossName]

    for _, uid in ipairs(getPlayersOnline()) do
        if getPlayerStorageValue(uid, boss.storage) > 0 then
            table.insert(players, uid)
        end
    end

    table.sort(players, function(a, b) return getPlayerStorageValue(a, boss.storage) > getPlayerStorageValue(b, boss.storage) end)

    local porcentagem = math.ceil(getPlayerStorageValue(players[1], boss.storage))

    for i = 1, table.maxn(players) do

        local tabela_reward = {}
        local pontos = getPlayerStorageValue(players[i], boss.storage)

        if i == 1 then
            addLoot(boss.comum, tabela_reward, false)
            addLoot(boss.semi_raro, tabela_reward, false)
            addLoot(boss.raro, tabela_reward, false)
            addLoot(boss.sempre, tabela_reward, true)
        elseif i >= 2 and pontos >= math.ceil((porcentagem * 0.8)) then
            addLoot(boss.comum, tabela_reward, false)
            addLoot(boss.semi_raro, tabela_reward, false)
            addLoot(boss.raro, tabela_reward, false)
            addLoot(boss.muito_raro, tabela_reward, false)
        elseif pontos < math.ceil((porcentagem * 0.8)) and pontos >= math.ceil((porcentagem * 0.6)) then
            addLoot(boss.comum, tabela_reward, false)
            addLoot(boss.semi_raro, tabela_reward, false)
            addLoot(boss.raro, tabela_reward, false)
        elseif pontos < math.ceil((porcentagem * 0.6)) and pontos >= math.ceil((porcentagem * 0.4)) then
            addLoot(boss.comum, tabela_reward, false)
            addLoot(boss.semi_raro, tabela_reward, false)
        elseif pontos < math.ceil((porcentagem * 0.4)) and pontos >= math.ceil((porcentagem * 0.1)) then
            addLoot(boss.comum, tabela_reward, false)
        end

        addRewardLoot(players[i], bossName, tabela_reward)
    end
end

function onDeath(cid, corpse, killer)
    local boss = REWARDCHEST.bosses[getCreatureName(cid):lower()]
    if boss then
        addEvent(rewardChestSystem, 1000, getCreatureName(cid):lower())
    end
    return true
end

function onStatsChange(cid, attacker, type, combat, value)
    if isMonster(cid) and type == STATSCHANGE_HEALTHLOSS and isPlayer(attacker) then
        local boss = REWARDCHEST.bosses[getCreatureName(cid):lower()]
        if boss and attacker then
            setPlayerStorageValue(attacker, boss.storage, getPlayerStorageValue(attacker, boss.storage) + math.ceil((value / REWARDCHEST.formula.hit)))
        end
    end
    return true
end
Rewardchest

Code:
REWARDCHEST = {
    rewardBagId = 2595,
    formula = {hit = 3, block = 1, suport = 9},
    storageExaust = 60000,
    town_id = 1,
    bosses = {
        ["ghazbaran"] =
        {
            comum = {
                {2143, 10, 100}, -- white pearl
                {2146, 10, 100}, -- small sapphire
                {2145, 10, 100}, -- small diamond
                {2144, 10, 100}, -- black pearl
                {2149, 10, 100}, -- small emeralds
                {5954, 3, 100}, -- demon horn
                {7896, 1, 100}, -- glacier kilt
                {7590, 1, 100}, -- great mana potion
                {2179, 1, 100}, -- gold ring
                {2151, 1, 100}, -- talon
            },

            semi_raro = {
                {2152, 90, 100}, -- platinum coin
                {7368, 50, 100}, -- assassin star
                {2130, 1, 100}, -- golden amulet
                {2436, 1, 100}, -- skull staff
                {2447, 1, 100}, -- twin axe
                {2171, 1, 100}, -- platinum amulet
                {2158, 1, 100}, -- blue gem
                {2393, 1, 100}, -- giant sword
                {2164, 1, 100}, -- might ring
                {7454, 1, 100}, -- glorious axe
                {6553, 1, 100}, -- ruthless axe
                {9971, 1, 100}, -- gold ingot
            },

            raro = {
                {2160, 3, 100}, -- crystal coin
                {8887, 1, 100}, -- frozen plate
                {2470, 1, 100}, -- golden legs
                {2472, 1, 100}, -- magic plate armor
                {7431, 1, 100}, -- demonbone
                {2520, 1, 100}, -- demon shield
                {5943, 1, 100}, -- morgaroth hearth
                {2466, 1, 100}, -- golden armor
                {2514, 1, 100}, -- mastermind shield
                {2142, 1, 100}, -- ancient amulet
                {2123, 1, 100}, -- ring of the sky
                {1984, 1, 100}, -- blue tome
            },

            muito_raro = {
                {8866, 1, 100}, -- robe of the ice queen
                {6553, 1, 100}, -- ruthless axe
                {8884, 1, 100}, -- oceanborn leviathan armor
                {2421, 1, 100}, -- thunder hammer
                {2646, 1, 100}, -- golden boots
                {7455, 1, 100}, -- mythril axe
            },
          
            sempre = {
                {2148, 90, 100}, -- gold coin
                {6500, 9, 100}, -- demonic essence
                {2177, 1, 100}, -- life crystal
                {2214, 1, 100}, -- ring of healing
            },
          
            storage = 65479,
        },
      
        ["orshabaal"] =
        {
            comum = {
                {2143, 10, 100}, -- white pearl
                {2146, 10, 100}, -- small sapphire
                {2145, 10, 100}, -- small diamond
                {2144, 10, 100}, -- black pearl
                {2149, 10, 100}, -- small emeralds
                {5954, 3, 100}, -- demon horn
                {7896, 1, 100}, -- glacier kilt
                {2432, 1, 100}, -- fire axe
                {2462, 1, 100}, -- devil helmet
                {7590, 1, 100}, -- great mana potion
                {2179, 1, 100}, -- gold ring
                {2151, 1, 100}, -- talon
            },

            semi_raro = {
                {2195, 1, 100}, -- boots of haste
                {2436, 1, 100}, -- skull staff
                {2393, 1, 100}, -- giant sword
                {5954, 5, 100}, -- demon horn
            },

            raro = {
                {2470, 1, 100}, -- golden legs
                {2472, 1, 100}, -- magic plate armor
                {2514, 1, 100}, -- mastermind shield
                {2520, 1, 100}, -- demon shield
                {1982, 1, 100}, -- purple tome
                {2123, 1, 100}, -- ring of the sky
            },

            muito_raro = {
                {8890, 1, 100}, -- Robe of the underworld
                {2421, 1, 100}, -- thunder hammer
            },
          
            sempre = {
                {5808, 1, 100}, -- orshabaals brain
                {2171, 1, 100}, -- platinum amulet
                {2148, 90, 100}, -- gold coin
                {2146, 10, 100}, -- small sapphire
            },
          
            storage = 65480,
        },
      
        ["ferumbras"] =
        {
            comum = {
                {2143, 10, 100}, -- white pearl
                {2146, 10, 100}, -- small sapphire
                {2145, 10, 100}, -- small diamond
                {2144, 10, 100}, -- black pearl
                {2149, 10, 100}, -- small emeralds
                {7416, 1, 100}, -- Bloody Edge
                {7896, 1, 100}, -- glacier kilt
                {2432, 1, 100}, -- fire axe
                {2462, 1, 100}, -- devil helmet
                {7590, 1, 100}, -- great mana potion
                {2179, 1, 100}, -- gold ring
                {2151, 1, 100}, -- talon
            },

            semi_raro = {
                {2195, 1, 100}, -- boots of haste
                {2472, 1, 100}, -- magic plate armor
                {2393, 1, 100}, -- giant sword
                {2470, 1, 100}, -- golden legs
                {2514, 1, 100}, -- mastermind shield
            },
            raro = {
                {8885, 1, 100}, -- divine plate
                {2520, 1, 100}, -- demon shield
                {8930, 1, 100}, -- emerald sword
                {2522, 1, 100}, -- great shield
                {2421, 1, 100}, -- thunder hammer
            },

            muito_raro = {
                {5903, 1, 100}, -- ferumbras hat
            },
          
            sempre = {
                {2171, 1, 100}, -- platinum amulet
                {2148, 90, 100}, -- gold coin
                {2146, 10, 100}, -- small sapphire
            },
          
            storage = 65481,
        },
        ["zulazza the corruptor"] =
        {
            comum = {
                {2158, 1, 100}, -- blue gem
                {2156, 1, 100}, -- red gem
                {2155, 1, 100}, -- green gem
                {2154, 1, 100}, -- yellow gem
                {2153, 1, 100}, -- violet gem
            },

            semi_raro = {
                {5944, 5, 100}, -- soul orb
            },

            raro = {
                {2514, 1, 100}, -- mastermind shield
            },

            muito_raro = {
                {11114, 1, 100}, -- dragon scale boots
                {8882, 1, 100}, -- earthborn titan armor
            },
          
            sempre = {
                {2152, 90, 100}, -- platinum coin
                {9971, 5, 100}, -- gold ingot
            },
          
            storage = 65482,
        },
        ["morgaroth"] =
        {
            comum = {
                {2143, 10, 100}, -- white pearl
                {2146, 10, 100}, -- small sapphire
                {2145, 10, 100}, -- small diamond
                {2144, 10, 100}, -- black pearl
                {2149, 10, 100}, -- small emeralds
                {5954, 3, 100}, -- demon horn
                {7896, 1, 100}, -- glacier kilt
                {2432, 1, 100}, -- fire axe
                {2462, 1, 100}, -- devil helmet
                {7590, 1, 100}, -- great mana potion
                {2179, 1, 100}, -- gold ring
                {2151, 1, 100}, -- talon
            },

            semi_raro = {
                {2195, 1, 100}, -- boots of haste
                {2393, 1, 100}, -- giant sword
                {5954, 5, 100}, -- demon horn
                {2123, 1, 100}, -- ring of the sky
            },

            raro = {
                {8886, 1, 100}, -- molten plate
                {2472, 1, 100}, -- magic plate armor
                {8867, 1, 100}, -- dragon robe
                {2514, 1, 100}, -- mastermind shield
                {2520, 1, 100}, -- demon shield
                {1982, 1, 100}, -- purple tome
                {8851, 1, 100}, -- Royal Crossbow
            },

            muito_raro = {
                {2421, 1, 100}, -- thunder hammer
                {2522, 1, 100}, -- great shield
                {8850, 1, 100}, -- chain bolter
            },
          
            sempre = {
                {5943, 1, 100}, -- morgaroths brain
                {2171, 1, 100}, -- platinum amulet
                {2148, 90, 100}, -- gold coin
                {2146, 10, 100}, -- small sapphire
            },
            storage = 65483,
        },
    }
}
reward pontos
Code:
dofile('data/sistemas/rewardchest.lua')

function onLogin(cid)
    for key, value in pairs(REWARDCHEST.bosses) do
        if getPlayerStorageValue(cid, value.storage) > 0 then
            setPlayerStorageValue(cid, value.storage, 0)
        end
    end
    registerCreatureEvent(cid, "RewardChestStats")
    return true
end

function onStatsChange(cid, attacker, type, combat, value)
    if isMonster(attacker) and (type == STATSCHANGE_HEALTHLOSS or type == STATSCHANGE_MANALOSS) then
        local boss = REWARDCHEST.bosses[getCreatureName(attacker):lower()]
        if boss then
            setPlayerStorageValue(cid, boss.storage, getPlayerStorageValue(cid, boss.storage) + math.ceil((value / REWARDCHEST.formula.block)))
            setPlayerStorageValue(cid, REWARDCHEST.storageExaust, os.time() + 5)
        end
    elseif (isPlayer(attacker) and (type == STATSCHANGE_HEALTHGAIN or type == STATSCHANGE_MANAGAIN) and (getCreatureHealth(cid) < getCreatureMaxHealth(cid)) and (getPlayerStorageValue(cid, REWARDCHEST.storageExaust) >= os.time())) then
        for key, valor in pairs(REWARDCHEST.bosses) do
            if getPlayerStorageValue(cid, valor.storage) > 0 then
                if getCreatureHealth(cid) + value > getCreatureMaxHealth(cid) then
                    local add = getCreatureMaxHealth(cid) - getCreatureHealth(cid)
                    setPlayerStorageValue(attacker, valor.storage, getPlayerStorageValue(attacker, valor.storage) + math.ceil((add / REWARDCHEST.formula.suport)))
                else
                    setPlayerStorageValue(attacker, valor.storage, getPlayerStorageValue(attacker, valor.storage) + math.ceil((value / REWARDCHEST.formula.suport)))
                end
            end
        end
    end
    return true
end
 
Last edited:

jo3bingham

Premium User
Premium User
Joined
Mar 3, 2008
Messages
980
Best answers
4
Likes
413
#2
Your error is happening on this line:
Code:
setPlayerStorageValue(attacker, boss.storage, getPlayerStorageValue(attacker, boss.storage) + math.ceil((value / REWARDCHEST.formula.hit)))
My guess is, more specifically, here:
Code:
(value / REWARDCHEST.formula.hit)
value shouldn't be a string, so it's more likely that REWARDCHEST.formula.hit is. However, you didn't include your rewardchest.lua code, so it's hard to say for certain.
 
Joined
Apr 26, 2008
Messages
349
Best answers
0
Likes
5
#3
Your error is happening on this line:
Code:
setPlayerStorageValue(attacker, boss.storage, getPlayerStorageValue(attacker, boss.storage) + math.ceil((value / REWARDCHEST.formula.hit)))
My guess is, more specifically, here:
Code:
(value / REWARDCHEST.formula.hit)
value shouldn't be a string, so it's more likely that REWARDCHEST.formula.hit is. However, you didn't include your rewardchest.lua code, so it's hard to say for certain.
Yes, is something wrong with this formula I think, but I dont know what... I put the reward chest
 

jo3bingham

Premium User
Premium User
Joined
Mar 3, 2008
Messages
980
Best answers
4
Likes
413
#4
Ok, so REWARDCHEST.formula.hit isn't a string so value must be. Use print(type(value)) to confirm it's the variable that's actually a string. Did you write this reward chest system, or are you using a public one? You might want to look at the code that calls onStatsChange and see if value is being passed as a string.
 

Stigma

Well-Known Member
Joined
Feb 14, 2015
Messages
4,363
Best answers
356
Likes
1,938
#5
in older versions storage values use strings
check the storage value using print(getPlayerStorageValue(attacker, boss.storage))
 
Joined
Apr 26, 2008
Messages
349
Best answers
0
Likes
5
#6
I'm using a public one @Jo3Bingham

Code:
 stack traceback:
data/creaturescripts/scripts/rewardchest_boss.lua:105: in function <data/creaturescripts/scripts/rewardchest_boss.lua:100>
12983534
12985205
21152557
21153224
└ e

[Error - CreatureScript Interface]
data/creaturescripts/scripts/rewardchest_boss.lua:onStatsChange
Description:
data/creaturescripts/scripts/rewardchest_boss.lua:105: attempt to perform arithmetic on a string value
stack traceback:
data/creaturescripts/scripts/rewardchest_boss.lua:105: in function <data/creaturescripts/scripts/rewardchest_boss.lua:100>
21153224
21161710
12986585
12987734
21161710
21162472
└ e
 

jo3bingham

Premium User
Premium User
Joined
Mar 3, 2008
Messages
980
Best answers
4
Likes
413
#8
In rewardchest_boss.lua, change your onStatsChange() method to this and then show me the output.
Code:
function onStatsChange(cid, attacker, type, combat, value)
    if isMonster(cid) and type == STATSCHANGE_HEALTHLOSS and isPlayer(attacker) then
        local boss = REWARDCHEST.bosses[getCreatureName(cid):lower()]
        if boss and attacker then
            local storageValue = getPlayerStorageValue(attacker, boss.storage)

            print('storageValue is ' .. storageValue .. ', and of type ' .. type(storageValue))
            print('value is ' .. value .. ', and of type ' .. type(value))

            -- We'll leave this so we can see when the error occurs after the prints above.
            setPlayerStorageValue(attacker, boss.storage, storageValue + math.ceil((value / REWARDCHEST.formula.hit)))
        end
    end
    return true
end
 
Joined
Apr 26, 2008
Messages
349
Best answers
0
Likes
5
#9
In rewardchest_boss.lua, change your onStatsChange() method to this and then show me the output.
Code:
function onStatsChange(cid, attacker, type, combat, value)
    if isMonster(cid) and type == STATSCHANGE_HEALTHLOSS and isPlayer(attacker) then
        local boss = REWARDCHEST.bosses[getCreatureName(cid):lower()]
        if boss and attacker then
            local storageValue = getPlayerStorageValue(attacker, boss.storage)

            print('storageValue is ' .. storageValue .. ', and of type ' .. type(storageValue))
            print('value is ' .. value .. ', and of type ' .. type(value))

            -- We'll leave this so we can see when the error occurs after the prints above.
            setPlayerStorageValue(attacker, boss.storage, storageValue + math.ceil((value / REWARDCHEST.formula.hit)))
        end
    end
    return true
end
Code:
 [Error - CreatureScript Interface]
data/creaturescripts/scripts/rewardchest_boss.lua:onStatsChange
Description:
data/creaturescripts/scripts/rewardchest_boss.lua:106: attempt to call local 'type' (a number value)
stack traceback:
data/creaturescripts/scripts/rewardchest_boss.lua:106: in function <data/creaturescripts/scripts/rewardchest_boss.lua:100>
 

jo3bingham

Premium User
Premium User
Joined
Mar 3, 2008
Messages
980
Best answers
4
Likes
413
#10
My apologies, I didn’t realize there was a parameter ‘type’. Just rename the parameter to something else and rename any uses of that parameter to the new name and try it again.
 
Joined
Apr 26, 2008
Messages
349
Best answers
0
Likes
5
#11
My apologies, I didn’t realize there was a parameter ‘type’. Just rename the parameter to something else and rename any uses of that parameter to the new name and try it again.
But I can use anything? I tried _type and I had the same problem. type isn't the param to the statschange?
 

Xikini

I whore myself out for likes
Joined
Nov 17, 2010
Messages
3,878
Best answers
122
Likes
2,010
#12
My apologies, I didn’t realize there was a parameter ‘type’. Just rename the parameter to something else and rename any uses of that parameter to the new name and try it again.
But I can use anything? I tried _type and I had the same problem. type isn't the param to the statschange?
I think he means like this.
Lua:
function onStatsChange(cid, attacker, _type, combat, value)
    if isMonster(cid) and _type == STATSCHANGE_HEALTHLOSS and isPlayer(attacker) then
        local boss = REWARDCHEST.bosses[getCreatureName(cid):lower()]
        if boss and attacker then
            local storageValue = getPlayerStorageValue(attacker, boss.storage)
   
            print('storageValue is ' .. storageValue .. ', and of type ' .. type(storageValue))
            print('value is ' .. value .. ', and of type ' .. type(value))
   
            -- We'll leave this so we can see when the error occurs after the prints above.
            setPlayerStorageValue(attacker, boss.storage, storageValue + math.ceil((value / REWARDCHEST.formula.hit)))
        end
    end
    return true
end
 
Joined
Apr 26, 2008
Messages
349
Best answers
0
Likes
5
#13
I tried this, the script was working good then this problem happened... maybe this had happened when more than 1 player start killing the boss.

Code:
value is 5037182, and of type number
storageValue is 3235067, and of type number
value is 4730396, and of type number
storageValue is └ , and of type string
value is 4688775, and of type number

[Error - CreatureScript Interface]
data/creaturescripts/scripts/rewardchest_boss.lua:onStatsChange
Description:
 data/creaturescripts/scripts/rewardchest_boss.lua:109: attempt to perform arithmetic on local 'storageValue' (a string value)
stack traceback:
data/creaturescripts/scripts/rewardchest_boss.lua:109: in function <data/creaturescripts/scripts/rewardchest_boss.lua:99>
storageValue is └ , and of type string
value is 4401966, and of type number
 

Xikini

I whore myself out for likes
Joined
Nov 17, 2010
Messages
3,878
Best answers
122
Likes
2,010
#14
mmm I wonder..
Is there a maximum value that the database allows storage values to hold?

When you are attacking the monsters, the storage value continues to add to itself and increases, correct?
So if there is a maximum value that the database is allowing for the storage value, it might be like.. overflowing?
and at that point it doesn't know what number the storage value is since it's changed to a symbol or nil..
which of course would cause the error we are seeing, as a symbol or nil would both not be a number.

Just spit-balling, since I don't know the correct/definite answer to your issue.
 

Stigma

Well-Known Member
Joined
Feb 14, 2015
Messages
4,363
Best answers
356
Likes
1,938
#15
the problem is here (looks like you put a value that corrupted it)
storageValue is └ , and of type string
not sure we can do much to help unless you show everywhere that you change boss.storage values
 
Joined
Apr 26, 2008
Messages
349
Best answers
0
Likes
5
#16
mmm I wonder..
Is there a maximum value that the database allows storage values to hold?

When you are attacking the monsters, the storage value continues to add to itself and increases, correct?
So if there is a maximum value that the database is allowing for the storage value, it might be like.. overflowing?
and at that point it doesn't know what number the storage value is since it's changed to a symbol or nil..
which of course would cause the error we are seeing, as a symbol or nil would both not be a number.

Just spit-balling, since I don't know the correct/definite answer to your issue.
Maybe was that, It was varchar 255, I had changed to varchar 2550 and the error keep occurring, now I changed to TEXT tested once and didn't have the error.

I'll do more tests and hope to dont have any troubles this change to text :D
 
Top