• 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 0.X show list of players with same storage

royalpala

Well-Known Member
Joined
Dec 31, 2019
Messages
85
Solutions
1
Reaction score
68
hello people, I was wondering if there's a way to make a function (action onUse) for a book to show a list of players with the same storage as you?
anybody got a clue?

example:
you have storage 9999 == 1 then show list of players with storage 9999 = 1



thanks in advance for your comments :)
 
Solution
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
    local text = ""
    for _, pid in ipairs(getPlayersOnline()) do
        if getCreatureStorage(pid, 9999) == 1 then
            if text ~= "" then
                text = text .. "\n"
            end
            text = text .. getCreatureName(pid)
        end
    end
    doShowTextDialog(cid, 1950, text)
    return true
end
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
    local text = ""
    for _, pid in ipairs(getPlayersOnline()) do
        if getCreatureStorage(pid, 9999) == 1 then
            if text ~= "" then
                text = text .. "\n"
            end
            text = text .. getCreatureName(pid)
        end
    end
    doShowTextDialog(cid, 1950, text)
    return true
end
 
Solution
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
    local text = ""
    for _, pid in ipairs(getPlayersOnline()) do
        if getCreatureStorage(pid, 9999) == 1 then
            if text ~= "" then
                text = text .. "\n"
            end
            text = text .. getCreatureName(pid)
        end
    end
    doShowTextDialog(cid, 1950, text)
    return true
end
You should use pairs over ipairs when you don't care about the order that the table is traversed in, it's faster. Also table.concat is a really fast & easy way to concatenate strings.

Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
    local players = {}
   
    for _, player in pairs(getPlayersOnline()) do
        if getCreatureStorage(player, 9999) == 1 then
            table.insert(players, getCreatureName(player))
        end
    end

    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, ("Players with storage:\n%s"):format(table.concat(players, ", ")))
    return true
end
 
You should use pairs over ipairs when you don't care about the order that the table is traversed in, it's faster. Also table.concat is a really fast & easy way to concatenate strings.

Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
    local players = {}
  
    for _, player in pairs(getPlayersOnline()) do
        if getCreatureStorage(player, 9999) == 1 then
            table.insert(players, getCreatureName(player))
        end
    end

    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, ("Players with storage:\n%s"):format(table.concat(players, ", ")))
    return true
end
and t[#t+1] = val is faster than table.insert
 
and t[#t+1] = val is faster than table.insert
Could also write it like this for maximum speed, but then it just looks gay, pairs & ipairs are both slower than indexing

Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
	local onlinePlayers = getPlayersOnline()
    local players = {}
   
    for i = 1, #onlinePlayers do
    	local player = onlinePlayers[i]
        if getCreatureStorage(player, 9999) == 1 then
            players[#players + 1] = getCreatureName(player)
        end
    end

    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, ("Players with storage:\n%s"):format(table.concat(players, ", ")))
    return true
end
 
can actually also keep track of index manually to make it even faster instead of using # operator each time
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
    local onlinePlayers = getPlayersOnline()
    local players = {}
    local index = 0
      
    for i = 1, #onlinePlayers do
        local player = onlinePlayers[i]
        if getCreatureStorage(player, 9999) == 1 then
            index = index + 1
            players[index] = getCreatureName(player)
        end
    end

    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, ("Players with storage:\n%s"):format(table.concat(players, ", ")))
    return true
end
 
can actually also keep track of index manually to make it even faster instead of using # operator each time
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
    local onlinePlayers = getPlayersOnline()
    local players = {}
    local index = 0
     
    for i = 1, #onlinePlayers do
        local player = onlinePlayers[i]
        if getCreatureStorage(player, 9999) == 1 then
            index = index + 1
            players[index] = getCreatureName(player)
        end
    end

    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, ("Players with storage:\n%s"):format(table.concat(players, ", ")))
    return true
end
Can also make the storage key & value local variables so they can be reused from a CPU register (not sure about this shit tbh)
 
Back
Top