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

TFS 1.X+ Query returning false

E

Evil Puncker

Guest
Okay so I have these functions:

Lua:
function Player.getAccountStorageValue(self, key)
    local toNumber = tonumber(key)
    if not toNumber then
        return false
    end
    local query = db.storeQuery("SELECT `value` FROM `account_storage` WHERE `account_id` = ".. self:getAccountId() .." AND `key` = ".. key)
    if not query then
        return false
    end

    local value = result.getNumber(query, "value")
    result.free(query)
    return value
end

function Player.setAccountStorageValue(self, key, value)
    local toNumber = tonumber(key)
    if not toNumber then
        return false
    end
    local query = ""
    if self:getAccountStorageValue(key) then
        query = ("UPDATE `account_storage` SET `value` = ".. value .." WHERE `account_id` = ".. self:getAccountId() .." AND `key` = "..key)
    else
        query = ("INSERT INTO `account_storage` (`account_id`, `key`, `value`) VALUES (".. self:getAccountId() ..", ".. key ..", ".. value ..")")
    end
    return db.query(query)
end

the issue:
I can't use
Lua:
player:getAccountStorageValue(123)
player:setAccountStorageValue(123,1)

because the first one will always return false and throw an "attempt to perform arithmetic on a boolean value " error
 
Solution
Okay so I have these functions:

Lua:
function Player.getAccountStorageValue(self, key)
    local toNumber = tonumber(key)
    if not toNumber then
        return false
    end
    local query = db.storeQuery("SELECT `value` FROM `account_storage` WHERE `account_id` = ".. self:getAccountId() .." AND `key` = ".. key)
    if not query then
        return false
    end

    local value = result.getNumber(query, "value")
    result.free(query)
    return value
end

function Player.setAccountStorageValue(self, key, value)
    local toNumber = tonumber(key)
    if not toNumber then
        return false
    end
    local query = ""
    if self:getAccountStorageValue(key) then
        query = ("UPDATE `account_storage` SET `value` = ".. value...
What line is the error throwing on?
Whats the code you are using that is throwing the error.

I've tested the code you posted and it works (I renamed the function as i already use a similar function).
1585502144165.png
 
Last edited:
What line is the error throwing on?
Whats the code you are using that is throwing the error.

I've tested the code you posted and it works (I renamed the function as i already use a similar function).
View attachment 43539
any scripts that use get first instead of set, throws the error

Lua:
local rewards = {
    {1, 36},
    {37, 46, 2148, 80},
    {47, 55, 2148, 50},
    {56, 64, 2671, 5},
    {65, 73, 2789, 5},
    {74, 81, 7620},
    {82, 87, 7618},
    {88, 92, 9811},
    {93, 96, 9808},
    {97, 100, 2213}
}

local crateUsable = Action()

function crateUsable.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if (os.time() - player:getAccountStorageValue(30114)) >= 3600 then
        player:setAccountStorageValue(30114, os.time())
        local chance = math.random(100)
        for i = 1, #rewards do
            local k = rewards[i]
            if chance >= k[1] and chance <= k[2] then
                if k[3] then
                    local item = ItemType(k[3])
                    local count = k[4] or 1
                    player:addItem(k[3], count)
                    local str = ("You found %s %s!"):format(count > 1 and count or item:getArticle(), count > 1 and item:getPluralName() or item:getName())
                    player:say(str, TALKTYPE_MONSTER_SAY, false, player, toPosition)
                else
                    player:say("You found nothing useful.", TALKTYPE_MONSTER_SAY, false, player, toPosition)
                end
                break
            end
        end
    else
        player:say("You found nothing useful.", TALKTYPE_MONSTER_SAY, false, player, toPosition)
    end
    return true
end

crateUsable:id(9661)
crateUsable:register()
 
If the value doesn't exist or the key is wrong it returns a false and not a value.

if (player:getAccountStorageValue(123)) then print(os.time() - player:getAccountStorageValue(123)) else print("error") end
 
Okay so I have these functions:

Lua:
function Player.getAccountStorageValue(self, key)
    local toNumber = tonumber(key)
    if not toNumber then
        return false
    end
    local query = db.storeQuery("SELECT `value` FROM `account_storage` WHERE `account_id` = ".. self:getAccountId() .." AND `key` = ".. key)
    if not query then
        return false
    end

    local value = result.getNumber(query, "value")
    result.free(query)
    return value
end

function Player.setAccountStorageValue(self, key, value)
    local toNumber = tonumber(key)
    if not toNumber then
        return false
    end
    local query = ""
    if self:getAccountStorageValue(key) then
        query = ("UPDATE `account_storage` SET `value` = ".. value .." WHERE `account_id` = ".. self:getAccountId() .." AND `key` = "..key)
    else
        query = ("INSERT INTO `account_storage` (`account_id`, `key`, `value`) VALUES (".. self:getAccountId() ..", ".. key ..", ".. value ..")")
    end
    return db.query(query)
end

the issue:
I can't use
Lua:
player:getAccountStorageValue(123)
player:setAccountStorageValue(123,1)

because the first one will always return false and throw an "attempt to perform arithmetic on a boolean value " error
Lua:
if not query then
        return false
end
If don't exist a result, return false. Change to return -1 to work like normal storages.
 
Solution
Back
Top