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

TalkAction [TFS 1.X] Get/Set Player Storage Value

Elwyn

Well-Known Member
Joined
Aug 24, 2014
Messages
212
Reaction score
76
Create a file in talkactions/scripts named storage.lua and put the following code:
Code:
-- Works for any TFS 1.X,
-- as long as versions after 1.2
-- maintain backward compatibility

function onSay(cid, words, param, type)
   local player = Player(cid)
   if not player or not player:getGroup():getAccess() then
     return true
   end

   if player:getAccountType() < ACCOUNT_TYPE_GAMEMASTER then
     return false
   end

   -- Extract the specified parameters.
   local parameters = param:split(",")

   if words == "/getstorage" and parameters[2] == nil then
     player:sendCancelMessage("Insufficient parameters, usage: !getstorage playerName, key")
     return false
   end

   if words == "/setstorage" and parameters[3] == nil then
     player:sendCancelMessage("Insufficient parameters, usage: !setstorage playerName, key, value")
     return false
   end

   -- Remove trailing/leading white spaces from parameters.
   local playerName = (parameters[1] or ""):trim()
   local storageKey = tonumber(parameters[2]) or 0

   -- Get meta player.
   local checkedPlayer = Player(playerName)
   if not checkedPlayer then
     player:sendCancelMessage(string.format("Could not find player '%s'.", playerName))
     player:getPosition():sendMagicEffect(CONST_ME_BUBBLES)

     return false
   end

   local storageValue = tonumber(parameters[3]) or checkedPlayer:getStorageValue(storageKey)
   local msg = string.format("Storage key '%s' %s set to '%d' for player '%s'.", storageKey, "%s", storageValue, checkedPlayer:getName())
   if words == "/setstorage" then
     -- Set specified storage value on player.
     checkedPlayer:setStorageValue(storageKey, storageValue)
     msg = string.format(msg, "is now")
   else
     -- Get specified storage value from player.
     msg = string.format(msg, "is currently")
   end

   -- Print the message in Local Chat in orange (only self can see).
   player:sendTextMessage(MESSAGE_EVENT_ORANGE, msg)
   player:getPosition():sendMagicEffect(CONST_ME_BUBBLES)
end

On talkactions.xml put the following tags:
Code:
  <talkaction words="/getstorage" separator=" " script="storage.lua"/>
   <talkaction words="/setstorage" separator=" " script="storage.lua"/>

To use /getstorage, just send /getstorage playerName, storage
To use /setstorage, just send /setstorage playerName, storage, value

The player must be online for it to work.
 
Last edited:
nice, however you dont need 2 scripts to do this, you can just make it /storage name, storage, [value]
with value being optional, so if you type
/storage zothion, 1000, 1
it will set 1000 to 1 for me
or if i use
/storage zothion, 1000
it will print out the storage which is now 1

all you need to do is check number of parameters and if 2 params then print~ else set~

for reference i use
Code:
function onSay(player, words, param)
    local split = param:split(",")
    local target = Player(split[1])
    local val = split[2]
    if #split == 2 then
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Storage number "..val.." of player "..split[1].." is: "..getPlayerStorageValue(target, val).."")
    elseif #split == 3 then
        setPlayerStorageValue(target, val, split[3])
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Storage number "..val.." of player "..split[1].." is now: "..getPlayerStorageValue(target, val).."")   
    else
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Invalid number of parameters")
    end
end
and it works fine, but it lacks fancy access checks etc ;)

also ur script is using cid and the likes of Player(cid), which is for <1.x and not 1.x+, for 1.x+ onSay already passes userdata of player as first param
 
That's true, I could even use words param to know still use /getstorage and /setstorage just for the sake of clarity. Thanks for the suggestion, I'll improve the script now :)
 
also while it works, i would not call the first param for onSay cid since it is not a creature id, but rather userdata of the person who says it in 1.x, so the Player(cid) is also unnecessary because it essentially gets userdata of player x from userdata of player x
 
also while it works, i would not call the first param for onSay cid since it is not a creature id, but rather userdata of the person who says it in 1.x, so the Player(cid) is also unnecessary because it essentially gets userdata of player x from userdata of player x
On TFS 1.0 it's the creature id, only after 1.1 it's the player user data sadly :(
 
On TFS 1.0 it's the creature id, only after 1.1 it's the player user data sadly :(
ah, didnt know that, never used 1.0, then its fine, i dont think Player(player userdata) will ever give an error anyway :)
 
You have to adapt it to work with all tfs >= 1.0 if you were to say "compatible with tfs 1.2". Curerntly it looks like it's compatible with tfs 1.0, maybe 1.1 if you change some functions. But yeah, that applies for everything. Also, I'd personally do like Zothion did, make one talkaction for both checking storages and setting storages. Anyways, nice job!
 
You have to adapt it to work with all tfs >= 1.0 if you were to say "compatible with tfs 1.2". Curerntly it looks like it's compatible with tfs 1.0, maybe 1.1 if you change some functions. But yeah, that applies for everything. Also, I'd personally do like Zothion did, make one talkaction for both checking storages and setting storages. Anyways, nice job!
Forgot to update the main post, but now it's as @Zothion suggested. It's compatible with every version of TFS until now, TFS 1.X is backward compatible so if it works for 1.0 it works for any other version.
 
maybe i shouldve mentioned, i tested it on my 1.2 when i saw Player(cid) to make sure Player(...) worked with a userdata value, and it did, so its fine @tokenzz :)

however i would still remove the 2 different commands for set/get storages, and just check length of the param:split instead
 
maybe i shouldve mentioned, i tested it on my 1.2 when i saw Player(cid) to make sure Player(...) worked with a userdata value, and it did, so its fine @tokenzz :)

however i would still remove the 2 different commands for set/get storages, and just check length of the param:split instead
It's just for clarity, I think it would be better to have those two commands so people don't mistakenly change a storage value when they meant to just read it
 
It's just for clarity, I think it would be better to have those two commands so people don't mistakenly change a storage value when they meant to just read it
fair enough, cant trust people these days ;)
 
Back
Top