• 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 how find string on monster name?

beenii

Well-Known Member
Joined
Jul 26, 2010
Messages
580
Solutions
1
Reaction score
57
i try
Code:
if string.find(""..target:getName().."", "[Legendary]") then

but dont work xD

i want find if monster name contain [Legendary]
 
Solution
Lua:
local string = "[Legendary] Some shit"
if string.find(string, "[Legendary]") then
    print("The monster is Legendary.")
else
    print("The monster isn't Legendary.")
end
Lua:
local string = "[Legendary] Some shit"
if string.find(string, "[Legendary]") then
    print("The monster is Legendary.")
else
    print("The monster isn't Legendary.")
end
 
Solution
To escape special characters in pattern matching you need to use "%" sign.

Lua:
local string = "[Legendary] Some shit"
if string.find(string, "%[Legendary%]") then
    print("The monster is Legendary.")
else
    print("The monster isn't Legendary.")
end
 
--------------------------------------------------------
SOLVED
-------------------------------------------------------

the problem was the capital letter of the beginning
 
Last edited:
pls do not name the variable "string", it gets confusing when you want to use string library but then pass string as a variable name
either way that should work, the escape pattern is correct
if your monster is named [Legendary] Orc or something it should work
 
pls do not name the variable "string", it gets confusing when you want to use string library but then pass string as a variable name
either way that should work, the escape pattern is correct
if your monster is named [Legendary] Orc or something it should work

I used a call to the name of the monster, and there I saw that the problem was the capital letter of the beginning :) thanks all
 
pls do not name the variable "string", it gets confusing when you want to use string library but then pass string as a variable name
either way that should work, the escape pattern is correct
if your monster is named [Legendary] Orc or something it should work
Well string isn't a keyword, reserved word or library, its a metatable ;) see :
Lua:
for k, v in pairs(string) do
      print(k, type(v))
end
Lua: demo
Lua:
unpack    function
len    function
match    function
gsub    function
format    function
rep    function
sub    function
packsize    function
lower    function
dump    function
byte    function
char    function
find    function
upper    function
pack    function
reverse    function
gmatch    function
 
Well string isn't a keyword, reserved word or library, its a metatable ;) see :
Lua:
for k, v in pairs(string) do
      print(k, type(v))
end
Lua: demo
Lua:
unpack    function
len    function
match    function
gsub    function
format    function
rep    function
sub    function
packsize    function
lower    function
dump    function
byte    function
char    function
find    function
upper    function
pack    function
reverse    function
gmatch    function

Ty for code
 
it seems as if you completely forgot (or never knew in the first place) what metatables are
string is a library, which is a regular table, not a metatable (see how your code to correct me only prints out the function name and then the function address, not any metamethods)
strings themselves ("abc") have metatables set to them with __index being string so you can use methods from the string library
fr1xzfnb.png

here's some code for you
Lua:
-- an actual metatable being printed
for k, v in pairs(getmetatable("")) do
    print(k, v)
end
output:
Code:
__index    table: 001D94D8
Lua: demo
 
Last edited:
it seems as if you completely forgot (or never knew in the first place) what metatables are
string is a library, which is a regular table, not a metatable (see how your code to correct me only prints out the function name and then the function address, not any metamethods)
strings themselves ("abc") have metatables set to them with __index being string so you can use methods from the string library
fr1xzfnb.png

here's some code for you
Lua:
-- an actual metatable being printed
for k, v in pairs(getmetatable("")) do
    print(k, v)
end
output:
Code:
__index    table: 001D94D8
Lua: demo
Congrats on your doodle... only took you an hour to make ;)
 
Congrats on your doodle... only took you an hour to make ;)
what's the problem with that? o,o

gotta sound smart because someone corrected you? :s

plx no excuse you are god coder we all know you are the one and only programmer in here. professor in computer science at the same time i suppose aff
 
Last edited:
Back
Top