• 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 Game.getSpectators Issue

Lava Titan

Developer
Joined
Jul 25, 2009
Messages
1,571
Solutions
3
Reaction score
98
Location
Portugal
Hey, I got this working fine in other scripts but no idea why it's not working in this one...
I made this script line by line, and I'm using the spectators function correctly I'm sure...
Can someone explain me what am I doing wrong please?

Thanks in advance :p

Using TFS 1.2

this is the error I'm getting
LOgxf0J.png


this is default TFS spectator func in compat.lua (I didn't touch it)
Code:
function getSpectators(centerPos, rangex, rangey, multifloor, onlyPlayers)
    local result = Game.getSpectators(centerPos, multifloor, onlyPlayers or false, rangex, rangex, rangey, rangey)
    if #result == 0 then
        return nil
    end

    for index, spectator in ipairs(result) do
        result[index] = spectator:getId()
    end
    return result
end

this is my script in movements
Code:
local config = {
    -- actionID, level, storage
    [13900] = {level = 5000, storage = 13900, storage_value = 1,set_storage = 1,msg_failure = "You must kill all devil's minions before take his throne.", msg_sucess = "Devil Yells:\nBEGONE INTRUDER!"},
    [13901] = {level = 5000, storage = 13901, storage_value = 1,set_storage = 0,msg_failure = "You must sit in devil's throne before acess this area.", msg_sucess = "Devil Yells:\nYOU WILL REGRET THIS!"}
}

function onStepIn(player, item, position, fromPosition)
    local item_aid = config[item:getActionId()]

    if player:getExhaustion(1000) > 0 then
        player:teleportTo(fromPosition)
    return true
    end
   
    if not player:isPlayer() then
    return true
    end

    if(not(item_aid))then
    return true
    end

    local center_pos = player:getPosition()
    local spectators = #Game.getSpectators(center_pos, false, false, 5, 5, 5, 5)
    local monsters = spectators:getMonsters()

    if item_aid.set_storage == 1 then
        if monsters == 0 and player:getStorageValue(item_aid.storage) == 0 then
            player:say(item_aid.msg_sucess, TALKTYPE_MONSTER_YELL)
            player:setStorageValue(item_aid.storage, item_aid.storage_value)
        elseif monsters >= 1 and player:getStorageValue(item_aid.storage) == 0 then
            player:say(item_aid.msg_failure, TALKTYPE_MONSTER_YELL)
            creature:teleportTo(fromPosition)
            player:setExhaustion(1000, 3)
        elseif monsters == 0 and player:getStorageValue(item_aid.storage) == 1 then
            player:say("You can't repeat this action.", TALKTYPE_MONSTER_YELL)
            creature:teleportTo(fromPosition)
            player:setExhaustion(1000, 3)
        end
    end
    if item_aid.set_storage == 0 then
        if player:getStorageValue(item_aid.storage) == item_aid.storage_value then
            player:say(item_aid.msg_sucess, TALKTYPE_MONSTER_YELL)
        else
            player:say(item_aid.msg_failure, TALKTYPE_MONSTER_YELL)
        end
    end
return true
end
 
Last edited:
i'm not sure but I think

Code:
getSpectators(centerPos, rangex, rangey, multifloor, onlyPlayers)

does same job as that one, since that one is inside the function but tested and it's not working also so they must be same, cuz it's same error

P.S: anyways, I updated script in post 1 with @president vankk edit
 
This is the problem or one of them.
Code:
local spectators = #Game.getSpectators(center_pos, false, false, 5, 5, 5, 5)
local monsters = spectators:getMonsters()
The # is known as the length operator.. it returns the length of a table or string, the return value is always a number and is either zero or greater than zero.
When you put # in front of Game.getSpectators, you made spectators a variable that holds a number value and not a table.
Since spectators's type is a number and not an object calling a method on a number value will return an error.

For more information on this please see below.
http://www.lua.org/pil/contents.html
http://tutorialspoint.com/lua/
 
Back
Top