• 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 if 5 players are in range then return false

ligus

New Member
Joined
Apr 26, 2010
Messages
253
Reaction score
0
Hello, I want to make script which returns false if there are 5 players in range

I'm trying to do it
Code:
function onCastSpell(cid, var)
    if(isInRange(getPlayerPosition(cid), {x = toPosition.x-7, y = toPosition.y-5, z = toPosition.z}, {x = toPosition.x+7, y = toPosition.y+5, z = toPosition.z})) then
        return false, doPlayerSendTextMessage(cid, MESSAGE_STATUS_SMALL, 'Sorry, not possible.'), doSendMagicEffect(getThingPos(cid), CONST_ME_POFF)
    end
    return doCombat(cid, combat, var)
end

Of course it does not work at all, this script needs to be fixed and should count players, does anyone know how can it be done?

Thanks in advance
 
Which tfs version are you using?

For tfs 1.0
Code:
local maxPlayers = 5

function onCastSpell(cid, var)
    local player = Player(cid)
    local specs = Game.getSpectators(player:getPosition(), false, false, 0, 7, 0, 7)
        for i = 1, #specs do
            if #specs >= maxPlayers then
            player:sendTextMessage(MESSAGE_STATUS_SMALL, 'Sorry, not possible.')
            player:getPosition():sendMagicEffect(CONST_ME_POFF)
            return false
        end
    end
        return true
enda
 
Try this:
Code:
local maxPlayers = 5

function onCastSpell(cid, var)
    local players = {}
    local specs = getSpectators(getCreaturePosition(cid), 7, 7, false)
        for i = 1, #specs do
            if isPlayer(specs[i]) then
                    table.insert(players, specs[i])
            end
        end
    if #players >= maxPlayers then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_SMALL, 'Sorry, not possible.')
                doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
        return false
    end
    return true
end
 
There is one problem because this line:
Code:
local specs = getSpectators(getCreaturePosition(cid), 7, 7, false)
counts players and NPC also, how can it be done to count players only? :p
 
Are you sure? This line should fixed that:
Code:
if isPlayer(specs[i]) then

Can you paste this under the
Code:
if isPlayer(specs[i]) then
Code:
print(getCreatureName(specs[i]))
 
Hmm, still does not work

Edit:
Code:
[20:40:40.752] data/spells/scripts/attack/wrath of nature.lua:15: attempt to get length of field '?' (a number value)
[20:40:40.755] stack traceback:
[20:40:40.755]  data/spells/scripts/attack/wrath of nature.lua:15: in function <data/spells/scripts/attack/wrath of nature.lua:11

15 line:
Code:
if #specs[i] >= maxPlayers then
 
No problem. But i think it will count you as a player in the spell, idk if you want that :)
Else you could do this:
Code:
if isPlayer(specs[i]) and specs[i] ~= cid then

Or just put number 6 on the max.
 
Back
Top