• 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 Get/Set Offline Player Storage Value

Orkanite

omfg mapper
Joined
May 11, 2008
Messages
365
Reaction score
115
Is there a way to get/set a player's particular storage value who is currently offline?

Here is my scrub storage talkaction that gets/sets online players' storage values
Lua:
function all_trim(s)
    return s:match( "^%s*(.-)%s*$" )
end

function onSay(cid, words, param, channel)
    if(param == "") then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Command param required.")
        return TRUE
    end

    --t1 playername
    --t2 get/set
    --t3 storageID
    --t4 (only used for "set" mode, value to set for storageID)

    local t = string.split(param, ",")
    local player = Creature(all_trim(t[1]))
    if (all_trim(t[2]) == "get") then
        local storageValue = player:getStorageValue(t[3])
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Player " .. all_trim(t[1]) .. "'s storage with ID " .. all_trim(t[3]) .. " is currently " .. storageValue)
    else
        if (all_trim(t[2]) == "set") then
            player:setStorageValue(t[3], t[4])
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Player " .. all_trim(t[1]) .. "'s storage with ID " .. all_trim(t[3]) .. " has been set to " .. all_trim(t[4]))
        else
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Unknown storage command.try 'get' and 'set' followed by comma followed by self storage id")
        end
    end
    return TRUE
end


I couldn't find this question clearly being asked and answered, please let me know and I or another can delete this thread.
 
as my friend mentioned, you have to appeal to DB. I haven't tested it, but it will be something like this:
Lua:
function all_trim(s)
    return s:match("^%s*(.-)%s*$")
end

function getPlayerIdByName(playerName)
    local resultId = db.storeQuery("SELECT `id` FROM `players` WHERE `name` = " .. db.escapeString(playerName))
    return resultId and result.getDataInt(resultId, "id") or 0
end

function getDatabaseStorageValue(playerId, storageId)
    local resultValue = db.storeQuery("SELECT `value` FROM `player_storage` WHERE `player_id` = " .. playerId .. " AND `key` = " .. storageId)
    return resultValue and result.getDataInt(resultValue, "value") or 0
end

function setDatabaseStorageValue(playerId, storageId, value)
    db.executeQuery("INSERT INTO `player_storage` (`player_id`, `key`, `value`) VALUES (" .. playerId .. ", " .. storageId .. ", " .. value .. ") ON DUPLICATE KEY UPDATE `value` = " .. value)
end

function onSay(cid, words, param, channel)
    if(param == "") then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Command param required.")
        return TRUE
    end

    local t = string.split(param, ",")
    local player = Creature(all_trim(t[1]))

    if player then
        if all_trim(t[2]) == "get" then
            local storageValue = player:getStorageValue(t[3])
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Player " .. all_trim(t[1]) .. "'s storage with ID " .. all_trim(t[3]) .. " is currently " .. storageValue)
        elseif all_trim(t[2]) == "set" then
            player:setStorageValue(t[3], t[4])
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Player " .. all_trim(t[1]) .. "'s storage with ID " .. all_trim(t[3]) .. " has been set to " .. all_trim(t[4]))
        else
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Unknown storage command. Try 'get' and 'set' followed by comma followed by self storage id")
        end
    else
        local playerName = all_trim(t[1])
        local playerId = getPlayerIdByName(playerName)
        
        if playerId > 0 then
            local playerStorageValue = getDatabaseStorageValue(playerId, all_trim(t[3]))

            if all_trim(t[2]) == "get" then
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Player " .. playerName .. "'s storage with ID " .. all_trim(t[3]) .. " is currently " .. playerStorageValue)
            elseif all_trim(t[2]) == "set" then
                setDatabaseStorageValue(playerId, all_trim(t[3]), t[4])
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Player " .. playerName .. "'s storage with ID " .. all_trim(t[3]) .. " has been set to " .. all_trim(t[4]))
            else
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Unknown storage command. Try 'get' and 'set' followed by comma followed by self storage id")
            end
        else
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Player " .. playerName .. " not found.")
        end
    end

    return TRUE
end
This code uses the db.storeQuery and db.executeQuery functions to communicate with the SQL database. Note that you need to adapt these features to your database environment.
 
Back
Top