• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Checking Radius for Monster

Cadyan

Well-Known Member
Joined
Mar 30, 2008
Messages
844
Reaction score
63
How can I check if there is a specific monster standing near my character?
Like, if there is a rat within 5 squares of me a message will say: "There is a rat near you!"

TFS 1.0
 
getSpectators(pos, rangex, rangey[, changeFloors = false]) - this will return a table with all creatures what are in the range from pos.
 
I don't know if any of this is different in TFS 1.0, but I'll type it out how I would do it in 0.3.6, hopefully you can use it to extract the information you need:

Code:
local creatures = getSpectators(pos, rangex, rangey[, changeFloors = false])
Now the table with all detected creatures is called 'creatures'.
If you put # in front of a table name, such as #creatures - this will return the number of entries in this table. So if the function found 5 monsters around you, #creatures would be 5.

Then using this, you can do:

Code:
for i = 1, #creatures do
if getCreatureName(creatures[i]) == "Rat" then
doCreatureSay(cid, "There is a Rat near me!", TALKTYPE_SAY)
end
end

This loop will go through all the creatures in the table, searching for those with the name Rat and printing the message every time a Rat is found.
 
Well, it can be easily made in TFS 1.0, but the problem is that it will spam, same creature name over and over again and also it doensn't stack the same creature into the text, Ex. 2x Rats and etc...

You have to find a way to store, specs:getId(), then return it false if that monster has been already "seen"(stored).

Code:
    local player = Player(cid)
    local specs = Game.getSpectators(player:getPosition(), multifloor, onlyPlayer, minRangeX, maxRangeX, minRangeY, maxRangeY)
    for i = 1, #specs do
        if Monster(specs[i]) then
            player:sendTextMessage(MESSAGE_INFO_DESCR, "There is an "..specs[i]:getName().." near you.")
        end
    end
 
Well, here you go:
Code:
    local player = Player(cid)
    local specs = Game.getSpectators(player:getPosition(), multifloor, onlyPlayer, minRangeX, maxRangeX, minRangeY, maxRangeY)
    for i = 1, #specs do
        if Monster(specs[i]:getId()) and specs[i]:getName() == "Rat" then
            player:sendTextMessage(MESSAGE_INFO_DESCR, "There is an Rat near you.")
        end
    end

Still should use this one, since its using metatables.
 
Last edited:
not sure what metatables are, if you could tell me that'd be so helpful :D
I've been getting better and better at .lua, but there is always more to learn
 
Well, it can be easily made in TFS 1.0, but the problem is that it will spam, same creature name over and over again and also it doensn't stack the same creature into the text, Ex. 2x Rats and etc...

You have to find a way to store, specs:getId(), then return it false if that monster has been already "seen"(stored).

Code:
    local player = Player(cid)
    local specs = Game.getSpectators(player:getPosition(), multifloor, onlyPlayer, minRangeX, maxRangeX, minRangeY, maxRangeY)
    for i = 1, #specs do
        if Monster(specs[i]) then
            player:sendTextMessage(MESSAGE_INFO_DESCR, "There is an "..specs[i]:getName().." near you.")
        end
    end

How would I go about this? "You have to find a way to store, specs:getId(), then return it false if that monster has been already "seen"(stored)."

Pls!!!!!!! Need dis

NVMM!!!!!

Figured it out by doing

if spectators[j]:getId() then
table.remove(spectators, j)
end

ezpz :)
 
Back
Top