TextLabel.Text Help

I’m not sure how to get a number from text label
I’m attempting to get the number from some random strings then the number
I’ve tried to print(tonumber()) but it returns with nil and I’m not sure how to get the value on its own now could someone help me

If there are any spaces or letters in the string it will return nil.
For example:

local myString = "3K"
print(tonumber(myString)) -- nil

However, if it’s something like this:

local myString = "3"
print(tonumber(myString)) -- 3

You will have to get all of the non-number characters out of there. I don’t really have any idea on how you could do this but check this out.

nil means that string isnt a number.

Few examples:

local abc = "123"
print(tonumber(abc)) -- prints 123

local abc = "hi 123"
print(tonumber(abc)) -- prints nil since it includes string and it cant convert to number

Fix is

local abc = "hi 123"
local getNumber = string.match(abc,"%d+")
print(tonumber(getNumber)) -- prints 123
1 Like

But how exactly would i get the number from the string example
local a = ‘m 12.31’

I’ve edited the post

Added the fix at the end.

1 Like