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

What is a Tonumber and Tostring used for?

So i see people use the things tonumber and tostring and i'm wondering how and why you use this please any answers if you know please post something.

1 comment

0

tostring() converts numbers to strings and tonumber() converts a string to a number if the string is only made of numbers

1 answer

tonumber() will convert a string to a number, if the string is only made of numbers. Here is an incorrect and correct example:

local string = "123123ab"
local numberFromString = tonumber(string) -- will error because there are characters in the string

local string = "123123"
local numberFromString = tonumber(string) -- it's good because the string has only numbers on it
print(numberFromString) -- prints 123123

tostring() does the inverse of tonumber(), it gets a number and turns it into a string:

local part = game.Workspace.Part
local stringFromNumber = tostring(part) -- its going to error as it's not a number

local number = 1234
local stringFromNumber = tostring(number) -- it's good because it is a number
print(stringFromNumber) -- wil print 1234

Hope this is a good explanation!

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