Getting only the first return value of the string.find function?

string.find returns 2 values, “where the occurrence starts and ends” but I just want the first one, in the number type, I know I could do:

print(tonumber(tostring(exampleString:find("p")):sub(1,1))) -- thhickkk parenthesis

But it only works if the return value has only 1 digit, if it returns 10 10 then I’ll be getting 1 instead of 10 that is my goal.

It seems like hard coding, and it doesn’t work 100% if a function returns 2 values there is no way of getting only the first?

Btw I tried searching that through google and the dev forum but had no luck with this.

1 Like

just wrap it in parenthesis

print( (exampleString:find("p")) )
3 Likes

Also to add on to @Synnwave’s reply, if you want to get the second returned value. You can just do this:

function returns2value()
 return 1,2
end

local a,b = returns2value()

print(b) -- "2"
1 Like

Sorry for the late answer, I was going to sleep but saw the notification on my phone so I turned my PC on just for this (it took really long to start) :joy:

Wrapping a function in parenthesis does that? How have I never heard of it?

Damn, I knew I was forgetting something, I always do this for functions that return 2 values but forgot I could do it just because it’s built-in.

Thank both of you! I’ll give the solution to @Synnwave as he answered first

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.