• 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 selecting x amount of players with storage

Sun

Knowledge is power - France is bacon
Joined
Jan 26, 2015
Messages
334
Solutions
22
Reaction score
249
How would I select x random players with the storage xxx?
I'm writing a ctf event in lua and trying to figure out how to(if one team has 2 or more players than the other) balance out the teams.

tfs 0.4
 
Use this fragment of code as an example of inserting, sorting and distributing.
Code:
local event = {
    { color = 'BLACK', outfit = {lookType = 134, lookHead = 114, lookBody = 114, lookLegs = 114, lookFeet = 114},
        text = "-BR-\nVocê entrou! Aguarde o ínicio do evento. Você precisa matar todos os players do time adversário e SOBREVIVER para obter a recompensa.\n\n-ENG-\nYou entered! Wait for the event start. You need to kill all the players of the opposing team and SURVIVE for win the reward.", destination = {x = 713, y = 763, z = 6}, storage = { p = 261, g = 262)
    },
    { color = 'RED', outfit = {lookType = 143, lookHead = 94, lookBody = 94, lookLegs = 94, lookFeet = 94},
        text = "-BR-\nVocê entrou! Aguarde o ínicio do evento. Você precisa matar todos os players do time adversário e SOBREVIVER para obter a recompensa.\n\n-ENG-\nYou entered! Wait for the event start. You need to kill all the players of the opposing team and SURVIVE for win the reward.", destination = {x = 656, y = 795, z = 6}, storage = { p = 260, g = 259)
    }
}
local oufits = {}
for i = 1, #event do
    outfits[event[i].color] = createConditionObject(CONDITION_OUTFIT)
    setConditionParam(outfits[event[i].color], CONDITION_PARAM_TICKS, -1)
    addOutfitCondition(outfits[event[i].color], event[i].outfit)
end

local centerOfEventRoom = {x = 0, y = 0, z = 0}
local eventRoom = {width = 7, height = 7}

--------------------------------------------------------------------------------------------------
    -- get everyone in the event room
    local players = getSpectators(centerOfEventRoom, eventRoom.width, eventRoom.height, false)
    local levels = {}
    local pid = {}
    -- create the teams
    local team = {}
    for i = 1, #event do
        team[event[i].color] = {}
    end
    -- get everyone's levels & their id's
    for i, cid in ipairs(players) do
        if isPlayer(cid) then
            pid[i] = cid
            table.insert(levels, getPlayerLevel(pid[i]))
        end
    end
    -- sort the players levels
    table.sort(levels, function(a, b) return a < b end)
    -- try to evenly place them on teams
    for x = 1, #levels do
        if getPlayerLevel(pid[x]) == levels[x] then
            team[x % 2 == 0 and event[1].color or event[2].color][x] = pid[x]
        end
    end
    -- teleport everyone to their destinations on their respective teams
    for i = 1, #event do
        for n = 1, #team[event[i].color] do
            local id = team[event[i].color][n]
            setPlayerStorageValue(id, event[i].storage.p, 1)
            setGlobalStorageValue(event[i].storage.g, getGlobalStorageValue(event[i].storage.g) + 1)
            doAddCondition(id, outfits[event[i].color])
            doTeleportThing(id, event[i].destination)
            doCreatureSetNoMove(id, true)
            oPlayerPopupFYI(id, event[i].text)
        end
    end
----------------------------------------------------------------------------------------------------------
    return true
end
 
care to go a bit more in detail? Read your thread and don't really understand how to use it to get the results I'm looking for.
say for example: red team has 20 players & white team has 24 players.
I want to loop it to send 1 player at a time and recalculating the players in each team.
Something like:
Code:
if getGlobalStorageValue(storages.whiteteam) > getGlobalStorageValue(storages.redteam) + 1 then
    for _, pid in ipairs(getPlayersOnline()) do
         i = getPlayerStorageValue(pid, storages.whiteplayer) - getPlayerStorageValue(pid, storages.redplayer)
         while i > 0 do
              if getPlayerStorageValue(pid, storages.whiteplayer) > 0 then
                  doTeleportThing(pid, redbase, false)
                  doSendMagicEffect(pid, getPlayerPosition(pid), 10)
                  doPlayerSetStorageValue(pid, storage.whiteplayer, -1)
                  doPlayerSetStorageValue(pid, storages.redplayer, 1)
              end
              i = i - 1
         end
     end
elseif getGlobalStorageValue(storages.redteam) > getGlobalStorageValue(storages.whiteteam) + 1 then
        do the same shit but for the other team
end

I haven't tested I'm pretty sure it would make all players switch teams and not just 1 random

Why don't you use tables to store the teams instead of using globalstorage+playerstorage?
It would make it easier for you to sort/equalize the tables.

But here it is your code fixed:
Code:
local whiten = getGlobalStorageValue(storages.whiteteam)
local redn = getGlobalStorageValue(storages.redteam)

if whiten > redn + 1 then
    for _, pid in ipairs(getPlayersOnline()) do
        local diff = whiten-redn
        if diff > 1 then
            if getPlayerStorageValue(pid, storages.whiteplayer) > 0 then
                doTeleportThing(pid, redbase, false)
                doSendMagicEffect(pid, getPlayerPosition(pid), 10)
                doPlayerSetStorageValue(pid, storage.whiteplayer, -1)
                doPlayerSetStorageValue(pid, storages.redplayer, 1)
                redn = redn+1
                whiten = whiten-1
            end
        else
            break
        end
    end
    setGlobalStorageValue(storages.whiteteam, whiten)
    setGlobalStorageValue(storages.redteam, redn)
elseif redn > whiten + 1 then
    -- do the same shit but for the other team
end
 
Back
Top