• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community
  • The forum is currently open to new registrations. The registration will close for a short period of time when reaching 500 active members, to upgrade the server resources.

Roblox How to use tonumber, but with text instead of numbers? (2 Viewers)

--[[So we all know that tonumber() is a function that takes a string number and turns it into a number, but for my game, it is critical for me to use the same thing, but with text. For instance, this code is an example of what I'm talking about.]]--

local textString = "HelloIAmLuaNerd!" --Name of a set-later variable with quotation marks (below)
local HelloIAmLuaNerd = game.Workspace.Value.Value --Setting a variable without the quotation marks

local toNumberText = tonumber(textString) --[[Trying to make the string variable "textString" into the variable "HelloIAmLuaNerd", by removing the quotation marks]]--

toNumberText = 15 --Changing the value with the toNumberText (defined above)

--[[So this is what I am trying to get to work, but it is not working so well, which is why I am here! I really hope you guys can help, as this will be very important, knowing that scoping and variables don't always work as they should in functions. Bye!]]--
 

stetre

Member
Rank: I
Joined
Jan 8, 2020
Messages
45
Reaction score
28
Location
Italy
Website
github.com
Ok, thanks

Let me elaborate: if you'd like people to spend their efforts to give you help, you'd better help them help you.
That is, try to tell them directly what problem are you trying to solve, not what solution you are trying to use.

Otherwise you ask them a double, if not triple, effort: reverse engineer a (perhaps wrong) solution to discover
the problem, explain why the proposed solution is wrong (if it is wrong), and maybe suggest a different solution.

In your case I presume that what you are trying to achieve is to avoid writing a long sequence of dot-separated
names each time you are accessing a nested field, such as game.Workspace.Value.Value. Am I correct?
 

Lua Nerd

Newcomer
Joined
Dec 25, 2020
Messages
3
Reaction score
0
Let me elaborate: if you'd like people to spend their efforts to give you help, you'd better help them help you.
That is, try to tell them directly what problem are you trying to solve, not what solution you are trying to use.

Otherwise you ask them a double, if not triple, effort: reverse engineer a (perhaps wrong) solution to discover
the problem, explain why the proposed solution is wrong (if it is wrong), and maybe suggest a different solution.

In your case I presume that what you are trying to achieve is to avoid writing a long sequence of dot-separated
names each time you are accessing a nested field, such as game.Workspace.Value.Value. Am I correct?
Yes, that's correct
 

stetre

Member
Rank: I
Joined
Jan 8, 2020
Messages
45
Reaction score
28
Location
Italy
Website
github.com
Yes, that's correct

Ok, then. The tonumber() function is of no use here, because it doesn't work with arbitrary strings but only with those that
can be converted to a number. Like, for example, the strings "123", "-51", "10e-36", and so on.

You can do this instead: you get a reference to the last table in the nesting, and use that to access its fields:

Code:
local t = game.Workspace.Value
print(t.Value) -- same as print(game.Workspace.Value.Value)
t.Value = 123 -- same as game.Workspace.Value.Value = 123

A word of caution: use the reference only as a temporary object, because if something changes up the chain it
looses its validity. For example:

Code:
local t = game.Workspace.Value
game.Workspace = a_different_workspace
-- now t is no more a reference to game.Workspace.Value
 
Top