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

{Solved} Attacking Area

Cadyan

Well-Known Member
Joined
Mar 30, 2008
Messages
845
Reaction score
63
I am getting errors like "attempt to index a boolean value". Any help?
Code:
function onCastSpell(cid, var)
    local PPOS = getPlayerPosition(cid)
    area1 = {x = PPOS.x, y = PPOS.y - 1, z = PPOS.z}
    area2 = {x = PPOS.x + 1, y = PPOS.y, z = PPOS.z}
    local players = {}
    for x = area1.x, area2.x do
        for y = area1.y, area2.y do
            for z = area1.z, area2.z do
                table.insert(players, getTopCreature({x = x, y = y, z = z}).uid)
            end
        end
    end
    area3 = {x = PPOS.x, y = PPOS.y + 1, z = PPOS.z}
    area4 = {x = PPOS.x - 1, y = PPOS.y, z = PPOS.z}
    for x = area3.x, area4.x do
        for y = area3.y, area4.y do
            for z = area3.z, area4.z do
                table.insert(players, getTopCreature({x = x, y = y, z = z}).uid)
            end
        end
    end
  
    if table.maxn(players) >= 1 then
        for i = 1, #players do
            if isPlayer(players[i]) then
                doTargetCombatHealth(0, players[i], COMBAT_PHYSICALDAMAGE, -1, -10, CONST_ME_STUN)
                return doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, 'You hit "..getPlayerByName(players[i]).."!')
            else
                doTargetCombatHealth(0, players[i], COMBAT_PHYSICALDAMAGE, -1, -10, CONST_ME_STUN)
                return doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, 'You hit a monster!')
            end
        end
    else
        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, 'No targets!')
    end
end
 
Last edited:
Code:
[17/01/2014 13:25:19] Lua Script Error: [Spell Interface] 
[17/01/2014 13:25:19] data/spells/scripts/test.lua:onCastSpell
[17/01/2014 13:25:19] LuaScriptInterface::luaIsPlayer(). Player not found
[17/01/2014 13:25:19] stack traceback:
[17/01/2014 13:25:19]     [C]: in function 'isPlayer'
[17/01/2014 13:25:19]     data/spells/scripts/test.lua:25: in function <data/spells/scripts/test.lua:1>
 
there is no check to see if this
Code:
getTopCreature({x = x, y = y, z = z}).uid
is actually a creature. you need to check if
Code:
getTopCreature({x = x, y = y, z = z}).uid > 0
before you add it in table. i think you are adding a bunch of zeros in your table, i could be wrong though hard when im not trying it myself
 
would this work?
Code:
if(isPlayer(getTopCreature({x = x, y = y, z = z}).uid)) or (isCreature(getTopCreature({x = x, y = y, z = z}).uid)) then
                    table.insert(players, getTopCreature({x = x, y = y, z = z}).uid)
                end
 
what version of tfs are you using.
and why do you do a bunch of looping to set players into tables, and then loop through the players one more time to deal the damage? cant you just not put them in tables and do your combat there instead.
 
so I assumed you wanted to do something with all 8 positions around you, if not you can edit the area and for loop according to how you want it.
Code:
local area = {
{x=-1, y=-1}, {x=0,y=-1}, {x=1, y=-1},
{x=-1, y=0},--[[YOU ARE HERE]]{x=1, y=0},
{x=-1, y=1}, {x=0, y=1}, {x=1, y=1},
}


function onCastSpell(cid, var)
    local PPOS = getPlayerPosition(cid)
    for i=1, #area do
        local tmp = {x=0,y=0,z=0}
        tmp.x = PPOS.x+area[i].x
        tmp.y = PPOS.y+area[i].y
        tmp.z = PPOS.z
      
        local target = getTopCreature(tmp).uid
        if(target > 0) then
            if(isPlayer(target)) then
                --DO WHATEVER WITH UR PLAYER target
            elseif(isMonster(target)) then
                --DO WHATEVER WITH UR MONSTER target
            end
               --PUT FANCY EFFECTS HERE IF YOU LIKE
        end
      
  end
    return true
end

tested on tfs 1.0 and works
 
I was trying for the 4 diretional squares, but 8 would work too. ill test this out in a bit
Will isCreature work in place of isMonster? I am not sure if Mystic Spirit 0.2.15 has isMonster
 
Last edited:
Ok it works, but it is only registering on 1 random monster around the player at a time. I am not sure if this will cause additional errors.
 
Last edited:
Bump - your version of the script is still only hitting 1 monster per cast, and I can't seem to fix it. And I added a message that says "no targets!", and it sometimes has multiple popups regardless of how many monsters surround me
 
Code:
local area = {
{x=-1, y=-1}, {x=0,y=-1}, {x=1, y=-1},
{x=-1, y=0},--[[YOU ARE HERE]]{x=1, y=0},
{x=-1, y=1}, {x=0, y=1}, {x=1, y=1},
}


function onUse(cid, item, position, itemEx, toPosition)
    local PPOS = getPlayerPosition(cid)
    local c = 0
    for i=1, #area do
        local tmp = {x=0,y=0,z=0}
        tmp.x = PPOS.x+area[i].x
        tmp.y = PPOS.y+area[i].y
        tmp.z = PPOS.z
     
        local target = getTopCreature(tmp).uid
        if(target > 0) then
            if(isPlayer(target)) then
                        broadcastMessage("player" .. getCreatureName(target))
                --DO WHATEVER WITH UR PLAYER target
            else
            broadcastMessage("monster" .. getCreatureName(target))
                --DO WHATEVER WITH UR MONSTER target
            end
            c = c+1
              --PUT FANCY EFFECTS HERE IF YOU LIKE
        end
     
  end
 
  if(c==0) then
    doPlayerSendCancel(cid, "No creatures to attack.")
  end
 
    return true
end

this should work for tfs 2.0 unless getTopCreature dosent return 0 if there is no creature. if it dosent work you need to figure out what getTopCreature returns, whether its nil, 0 or false and construct the if check accordingly
 
It all works, except:
1) the fact that it only targets 1 monster at a time
2) if there are no targets, I get 8 pop-up messgaes instead of 1.
 
Back
Top