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

Need a function!

Joined
Apr 17, 2008
Messages
1,922
Solutions
1
Reaction score
188
Location
Venezuela
Hello, i need a function like "getPlayersInArea(area)" and use like this:

Code:
local area = { fromx = XXX, fromy = YYY, fromz = ZZZ, tox = XXX, toy = YYY, toz = ZZZ }

local playersInArea = getPlayersInArea(area)

if playersInArea > 1 then --If in the area there is more than one player then....

setPlayerStorageValue(pid, storage, count)

else

doPlayerSendTextMessage(pid,22,"You can enter there")

end

It is that possible?


Thanks =)
 
Last edited:
Depends on how big area is :)

I'd do it this way:
Code:
function getPlayersFromArea(fromPos, toPos, callback)
    if type(callback) == 'function' then
        for _, player in ipairs(getPlayersOnline()) do
            if isInArea(player, fromPos, toPos) == TRUE then
                if callback(player) == FALSE then
                    return FALSE
                end
            end
        end

        return TRUE
    else
        local players = {}
        for _, player in ipairs(getPlayersOnline()) do
            if isInArea(player, fromPos, toPos) == TRUE then
                table.insert(players, player)
            end
        end

        return players
    end
end

2 ways of using it...

1.
Code:
local playersInArea = getPlayersFromArea(fromPos, toPos)
if #playersInArea > 0 then
    -- code
else
    -- code
end

Or:
Code:
local function callback(cid)
    -- do something with player in area O.o
end

if(getPlayersFromArea(fromPos, toPos, callback) == TRUE) then
    -- success
else
    -- fail, this will occur when returning FALSE in callback function...
end
 
And if i want teleport all players in the area, what i need to do?

it can be used this way?

Code:
local playersInArea = getPlayersFromArea(fromPos, toPos)

for i, pid in ipairs(playersInArea) do

if #playersInArea > 1 then

doTeleportThing(pid, pos, FALSE)

else

return FALSE

end
end
 
Yes you can, but that's why I added callback :)

Code:
local function callback(cid)
    if getPlayerGroupId(cid) < 2 then
        doTeleportThing(cid, pos, FALSE)
    end
end

if(getPlayersFromArea(fromPos, toPos, callback) == TRUE) then
    -- success
else
    -- fail, this will occur when returning FALSE in callback function...
end

But you don't need that if-statement, that's only if you'd use more advanced code :p

So it could be "shortened" to:

Code:
local function callback(cid)
    if getPlayerGroupId(cid) < 2 then
        doTeleportThing(cid, pos, FALSE)
    end
end

getPlayersFromArea(fromPos, toPos, callback)

Or optionally
Code:
getPlayersFromArea(fromPos, toPos, function(cid)
    if getPlayerGroupId(cid) < 2 then
        doTeleportThing(cid, pos, FALSE)
    end
end)

You can change parameter to "pid" if you want too, you mustnt write "cid" :D
Also if you use this inside of your script you will be able to use all variables in the scope, otherwise you will only reach global variables.
 
Back
Top