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

Solved Account Storage Funcion

Caduceus

Unknown Member
Joined
May 10, 2010
Messages
321
Solutions
2
Reaction score
24
I know that their are several Account storage functions floating around. None of which work. When I try to use this function as player:getAccountStorage() I am getting an error in console as player is a nil value on line "local playerAccount = player:getAccountId()". Player is defined above, I thought. Maybe I'm wrong.

Lua:
function Player.getAccountStorage(accountId, key)
local player = Player(cid)
    if player == nil then
        return false
end
    local playerAccount = player:getAccountId()
    local resultId = db.getResult("SELECT `value` FROM `account_storage` WHERE `account_id` = " .. playerAccount .. " and `key` = " .. db.escapeString(key))
    if resultId ~= false then
        local value = result.getDataInt(resultId, "value")
        result.free(resultId)
    return value
    end
    return -1
end

Also, when I try to log in. My character logs in & back out. not allowing me to enter game. All I have is a creaturescript to test onLogin, which storage will return.
Code:
function onLogin(player)
local accountId = player:getAccountId()
    print(player:getAccountStorage(accountId, xxx))
end
 
Do you have a account_storage table inside your database? If you do, then share with us how it looks. I tried it myself by changing account_storage to another value and the script worked for me.

You should also remove

accountId

from

Player.getAccountStorage(accountId, key)

Since it is never used!
(it gets automatically pulled from player here "local playerAccount = player:getAccountId()"
 
Do you have a account_storage table inside your database
Code:
CREATE TABLE IF NOT EXISTS `account_storage` (
  `account_id` int(11) NOT NULL DEFAULT '0',
  `key` int(10) unsigned NOT NULL DEFAULT '0',
  `value` varchar(255) NOT NULL DEFAULT '0',
  PRIMARY KEY (`account_id`,`key`),
  CONSTRAINT `account_storage_ibfk_1` FOREIGN KEY (`account_id`) REFERENCES `accounts` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

I tried it myself by changing account_storage to another value and the script worked for me.
Can you explain further what you mean here?
 
your function doesn't include a self parameter, you're also checking if player exists for no reason (you can't execute a player method on something that doesn't exist, so it must exist if the function runs)
Lua:
function Player.getAccountStorage(self, key)
    local playerAccount = self:getAccountId()
    local resultId = db.getResult("SELECT `value` FROM `account_storage` WHERE `account_id` = " .. playerAccount .. " and `key` = " .. db.escapeString(key))
    if resultId ~= false then
        local value = result.getDataInt(resultId, "value")
        result.free(resultId)
    return value
    end
    return -1
end

also, here's my approach at account storage values if you've never tried it [TFS 1.x] player:getAccountStorageValue | player:setAccountStorageValue
make sure to look at apollos' sql query and use that instead of the one i posted
 
you're also checking if player exists for no reason

I am trying to use the function in compat, so it can be used globally. When I do I get this.

Code:
Lua Script Error: [CreatureScript Interface]
data/creaturescripts/scripts/custom/promo.lua:onLogin
data/lib/compat/compat.lua:1057: attempt to index global 'player' (a nil value)
stack traceback:
        [C]: in function '__index'
        data/lib/compat/compat.lua:1057: in function 'getAccountStorage'
        data/creaturescripts/scripts/custom/accountStorageTest.lua:3: in function <data/creaturescripts/scripts/custom/accountStorageTest.lua:1>
 
I am trying to use the function in compat, so it can be used globally. When I do I get this.

Code:
Lua Script Error: [CreatureScript Interface]
data/creaturescripts/scripts/custom/promo.lua:onLogin
data/lib/compat/compat.lua:1057: attempt to index global 'player' (a nil value)
stack traceback:
        [C]: in function '__index'
        data/lib/compat/compat.lua:1057: in function 'getAccountStorage'
        data/creaturescripts/scripts/custom/accountStorageTest.lua:3: in function <data/creaturescripts/scripts/custom/accountStorageTest.lua:1>
check my edited message, i swapped player for self so that it works correctly
 
Back
Top