How do I take a number out of a string?

How do I take a number out of a string

Example: ‘bob34bd’

return 34

Thanks, lux

2 Likes

You can use string patterns, use the %d flag to match the first digit, and %d+ flag to match all digits:

local str = "bob34bd"
local nums = string.match(str, "%d+")
print(nums)
6 Likes

@BMWLux The best way to do this would be to use the function Roblox provides called tonumber() which converts strings to numbers.

Example:

local NumberString = "10"

local Number = tonumber(NumberString) -- This variable would be a number now.

1 Like

By the way if you have numbers seperated by more words only the first pair of digits will be selected, here’s how you can get all of them:

local str = "bob34bd24"
local nums = string.gmatch(str, "%d+")
for digit in nums do 
    print(digit) 
end
1 Like

Not sure if you understood OP’s question, look at his provided example, he wasn’t trying to convert a number in a string to a number, however, trying to take a number in a string (that contains letters and words aswell) out.

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