• 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.3] account storage table not working as it should?

E

Evil Puncker

Guest
Okay this is a followup to my other thread, but since this is another problem and that one was already solved, I thought it is better to open a new topic so here I am, the functions from the other thread, and the following script:

Lua:
local juiceSquizer = Action()

function juiceSquizer.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if (os.time() - player:getAccountStorageValue(30115)) >= 72000 then
        player:addItem(22728, 1)
        print(os.time() - player:getAccountStorageValue(30115))
        print(os.time() - player:getStorageValue(30115))
        player:setStorageValue(30115, os.time())
        player:setAccountStorageValue(30115, os.time())
        print(30115, os.time())
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You have found a tinder box.")
    else
        player:sendCancelMessage("The pile of bones is empty.")
    end
    return true
end

juiceSquizer:aid(6000)
juiceSquizer:register()

the issue:
the first print (using getaccountstoragevalue) prints 1585508212, thus making the if check not working as intended
the second print (using getstoragevalue) prints 51, which is right and makes the if check works great
the third print, prints 30115 1585508211

and I don't know what is wrong with these functions, they were supposed to work the same (print the same as the default getstoragevalue), but they print that big number instead, help me to figure this out 🤔

SQL:
CREATE TABLE IF NOT EXISTS `account_storage` (
  `account_id` int(4) NOT NULL DEFAULT '0',
  `key` int(10) unsigned NOT NULL DEFAULT '0',
  `value` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`account_id`,`key`),
  FOREIGN KEY (`account_id`) REFERENCES `accounts`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB;
--
-- Constraints for table `account_storage`
--
ALTER TABLE `account_storage`
  ADD CONSTRAINT `account_storage_ibfk_1` FOREIGN KEY (`account_id`) REFERENCES `accounts` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;


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 -1
    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
 
Last edited by a moderator:
Solution
It's this line if self:getAccountStorageValue(key) then in Player.setStorageValue. -1 evaluates as "true", so it tries to run UPDATE instead of INSERT even when the value does not exist in the database.

Just change it to if self:getAccountStorageValue(key) ~= -1 then and it works like a charm.

Lua:
function Player.setAccountStorageValue(self, key, value)
    local toNumber = tonumber(key)
    if not toNumber then
        return false
    end
    local query = ""
    if self:getAccountStorageValue(key) ~= -1 then
        query = ("UPDATE `account_storage` SET `value` = ".. value .." WHERE `account_id` = ".. self:getAccountId() .." AND `key` = "..key)
    else
        query = ("INSERT INTO `account_storage` (`account_id`...
player:getAccountStorageValue(30115) is returning -1.
Simple maths: 1 - (-1) = 2
1585508211 - (-1) = 1585508212

It should work the way you want if you set the account storage like you do with the player storage.
 
player:getAccountStorageValue(30115) is returning -1.
Simple maths: 1 - (-1) = 2
1585508211 - (-1) = 1585508212

It should work the way you want if you set the account storage like you do with the player storage.
oh my bad, I forgot to edit the script on the first post, I actually used

player:getAccountStorageValue(30115) and player:setAccountStorageValue(30115, os.time())

and

player:getStorageValue(30115) and player:setStorageValue(30115, os.time())

for testing, the later works fine while the other don't
 
I'm a bit confused why you are setting os.time in the storage, then subtracting it in the if statement.
Why not set the new time in there and then compare that time.
Also you could have the getaccountstoragevalue return a 0 instead of -1

Lua:
local juiceSquizer = Action()

function juiceSquizer.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if (player:getAccountStorageValue(30115)) <= os.time() then
        player:addItem(22728, 1)
                print(os.time() - player:getAccountStorageValue(30115))
                print(os.time() - player:getStorageValue(30115))
        player:setStorageValue(30115, os.time()+72000)
        player:setAccountStorageValue(30115, os.time()+72000)
                print(30115, os.time())
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You have found a tinder box.")
    else
        player:sendCancelMessage("The pile of bones is empty.")
    end
    return true
end

juiceSquizer:aid(6000)
juiceSquizer:register()

or
Lua:
local juiceSquizer = Action()
function juiceSquizer.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if (player:getAccountStorageValue(30115)) >= os.time() then
        return player:sendCancelMessage("The pile of bones is empty.")
    end

        player:addItem(22728, 1)
        player:setStorageValue(30115, os.time()+72000)
        player:setAccountStorageValue(30115, os.time()+72000)
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You have found a tinder box.")
    return true
end

juiceSquizer:aid(6000)
juiceSquizer:register()
 
Last edited:
It's this line if self:getAccountStorageValue(key) then in Player.setStorageValue. -1 evaluates as "true", so it tries to run UPDATE instead of INSERT even when the value does not exist in the database.

Just change it to if self:getAccountStorageValue(key) ~= -1 then and it works like a charm.

Lua:
function Player.setAccountStorageValue(self, key, value)
    local toNumber = tonumber(key)
    if not toNumber then
        return false
    end
    local query = ""
    if self:getAccountStorageValue(key) ~= -1 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
 
Solution
Back
Top