• 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 getSpectators error

Zazeros

Member
Joined
Feb 13, 2012
Messages
67
Reaction score
17
0.4

Hi guys, I am trying to learn more about getSpectators to make creative spells for monsters.

I tried to make a simple script that would do the following: teleport the monster to a random player near it and start attacking that player, changing its target.
Code:
function onCastSpell(cid, creaturePos, level, maglv, var)
  local target = getSpectators(creaturePos, 2, 2, 2, false)
  if target == nil or type(target) ~= "table" or #target == 0 then
    return false
  end
  target = target[math.random(#target)]
  if isPlayer(target) == true then
    local targetPos = getThingPos(target)
    if isWalkable(targetPos, false, false, false) then
      doTeleportThing(cid, targetPos)
      doSetCreatureTarget(cid, target)
      return true
    end
  end
  return false
end
But I keep getting this error:
attempt to index a number value

Can someone help?
Thank you.
 
your onCastSpell parameters are messed up.

It's normally just (cid, var) ?

So because of that your getSpectators function is using a bad variable.

change to
Lua:
function onCastSpell(cid, var)
local creaturePos = getThingPosition(cid)
--local target = getSpectators(creaturePos, 2, 2, 2, false) -- I think there is an extra '2' in here?
local target = getSpectators(creaturePos, 2, 2, false) -- use this one instead

I would also use a custom function to grab only players.. so the spell doesn't fail randomly by selecting a monster/player at random and failing the player check.

credit to Gesior.pl
Lua:
function getPlayerSpectators(centerPos, rangeX, rangeY, multifloor)
   multifloor = multifloor and true or false -- multifloor is optional, default 'false'
   local list = {}
   local spectators = getSpectators(centerPos, rangeX, rangeY, multifloor)
   if spectators then -- spectators may be 'nil' on TFS 0.4
      for _, cid in pairs(spectators) do
         if isPlayer(cid) then
            table.insert(list, cid)
         end
      end
   end

   return list
end
 
@Xikini Sorry for taking so long.

Yeah, I think it is... it isn't?

Sorry, I didn't test it right, now it worked, but not the function to change the monster's target to the player it teleported

I don't think that function exists.
Try using doMonsterSetTarget(cid, target)
 
Back
Top