Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
2
Answered by 4 years ago

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

0
thx bro speedyfox66 237 — 4y
0
no problem speedy 123nabilben123 499 — 4y
Ad
Log in to vote
2
Answered by
ScuffedAI 435 Moderation Voter
4 years ago

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

0
just pretend i accepted this cuz i cant accept multiple at once speedyfox66 237 — 4y

Answer this question