• 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 :gmatch() [TFS 1.2]

Aeronx

Intermediate OT User
Joined
Dec 17, 2015
Messages
735
Solutions
9
Reaction score
119
Hello otlanders!

I am trying to learn a bit more here, so maybe you can instruct me.

I have started working with :gmatch and for easy tests I tested it with monster names.

Lua:
local desc = target:getName():gmatch('%d')
local lvl = tonumber(desc())
print(lvl)

the targeted monster has the name "Monster 1".

But i get error in "tonumber (value expected)", so i guess, my gmatch is not correct. What am I doing wrong?

Thank you for your time!
 
Solution
damn.. its working.. the problem was monster name was monster [1]

So for more testing, i went farther.

Now monster name is monster [ii. 1]
so i changed the code like this

Lua:
local target = 'Monster [ii. 1]'
local desc = target:gmatch('[ii. %d]')
local lvl = tonumber(desc())
print(lvl)

but it prints nil, what should i do in this case? Thank you!

EDIT: nvm i solved it myself. gmatch should be the same ('%d')
And what do you want to retrieve? Just the number? Then the same code should work for you, do not change the regex.

If you want to get the complete text in the brackets, use this regex: target:gmatch('%[ii%.%s%d%]')
Hello otlanders!

I am trying to learn a bit more here, so maybe you can instruct me.

I have started working with :gmatch and for easy tests I tested it with monster names.

Lua:
local desc = target:getName():gmatch('%d')
local lvl = tonumber(desc())
print(lvl)

the targeted monster has the name "Monster 1".

But i get error in "tonumber (value expected)", so i guess, my gmatch is not correct. What am I doing wrong?

Thank you for your time!
Could you print target:getName() ?

I tried in Lua DEMO and for me its working...
Lua: demo
Code:
local target = 'Monster 1'
local desc = target:gmatch('%d')
local lvl = tonumber(desc())
print(lvl)

PS if you want more than 1 digir, use '%d+'
 
damn.. its working.. the problem was monster name was monster [1]

So for more testing, i went farther.

Now monster name is monster [ii. 1]
so i changed the code like this

Lua:
local target = 'Monster [ii. 1]'
local desc = target:gmatch('[ii. %d]')
local lvl = tonumber(desc())
print(lvl)

but it prints nil, what should i do in this case? Thank you!

EDIT: nvm i solved it myself. gmatch should be the same ('%d')
 
damn.. its working.. the problem was monster name was monster [1]

So for more testing, i went farther.

Now monster name is monster [ii. 1]
so i changed the code like this

Lua:
local target = 'Monster [ii. 1]'
local desc = target:gmatch('[ii. %d]')
local lvl = tonumber(desc())
print(lvl)

but it prints nil, what should i do in this case? Thank you!

EDIT: nvm i solved it myself. gmatch should be the same ('%d')
And what do you want to retrieve? Just the number? Then the same code should work for you, do not change the regex.

If you want to get the complete text in the brackets, use this regex: target:gmatch('%[ii%.%s%d%]')
 
Last edited:
Solution
Back
Top