Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is there any way to convert number "string" to a number "int" value?

I know this isn't needed in any situation really, but i just wanted to know because i made a script and when i try to compare leaderstas points (int), it will say "

18:39:38.491 - LocalScript:10: attempt to compare string with userdata

" (and yes i removed some of the error message to clear unneeded text)

anyways like i said, i already know that you can just set an int value into the UI and do more magic, but i wondered if there is any way to actually convert it.

2 answers

✓ Accepted answer

If you wanted to convert a string to a number you would use tonumber(). tonumber() takes in a string and converts it into a number, simple. You need to check if the string has only numbers or not.

Once you have the converted number, you can do anything like it just like a real number.

Here's an example:

local myString = "12344"

local myConvertedNumber = tonumber(myString)
print(myConvertedNumber)
-- > 12344

You can even turn numbers back to strings using tostring()

local IdontLikethisNumber = 64332

local ILikethisString = tostring(IdontLikeThisNumber)
print(ILikeThisString)
-- > "64332"

Hope this helped you

2 comments

0

thx bro

0

no problem speedy

Something that I'd like to add as an answer is to not compare the object itself but rather the property value of the object.

-- let's assume this is your points object
local points = Instance.new('IntValue') 

-- Example of what not to do:
if (points > 10) then

end

-- What to do instead
if (points.Value > 10) then

end

1 comment

0

just pretend i accepted this cuz i cant accept multiple at once

This discussion is preserved as it was. New answers, comments, and votes are closed.