(i am trying to get an textbox input into a instance)
I want to make a way where you could basically do:
print(unstring("game.Name"))
and it would print “Baseplate” or something like that.
I know tonumber but it doesn’t work when it’s not a number and returns nil.
I looked on google but found nothing.
Any help appreciated.
NilFloat
(NilFloat)
August 10, 2020, 6:56pm
#2
So you want to create function which is going to create Instance out of a string?
1 Like
It basically takes “game.Name” and returns “Baseplate”.
NilFloat
(NilFloat)
August 10, 2020, 6:58pm
#4
I am kinda confused since services are parent of a game and how it should then possibly return “Baseplate”?
The name of game is baseplate and its case sensitive because of what the game name is.
Subsz
(Sub)
August 10, 2020, 6:59pm
#6
Do you mean like, running code with a string? eg :
"print("hello")"
and then it runs the code inside the string?
Not that I don’t mean loadstring but I mean
print(unstring("game.Name")) --returns the name of the game
Subsz
(Sub)
August 10, 2020, 7:05pm
#8
Well you can do something similar with loadstring
print(loadstring("return game.Name")())
Sorry if I misunderstood
xLawOut
(xLawOut)
August 10, 2020, 7:07pm
#9
Yes.
You could have found the result if you phrased it differently, e.g. “convert string to instance”.
There’s no official function for it, however, someone made a clever snippet of code which should do exactly what you want:
local String = "" -- your string
local segments = String:split(".")
local instance = game --location to search
for i,v in pairs(segments) do
instance = instance[v]
end
print(instance.ClassName,typeof(instance)) -- ClassName, "Instance"
EDIT: I decided to add onto this and turn it into a function:
local String = "" -- your string
local function Unstring(str)
local instance = game
local segments = str:split(".")
for i,v in pairs(segments) do
instance = instance[v]
end
segments = nil
return instance
end
print(typeof(Unstring(String))) -- Instance
1 Like
Please note that loadstring is limited to the server only
nicemike40
(nicemike40)
August 10, 2020, 7:51pm
#12
Why are you trying to do this?
There’s almost definitely a better way to achieve whatever it is you’re trying to achieve.
Also, enabling loadstring
could open your game up for all sorts of exploits. On top of that, its slooooow.
happle5222
(DevHapple)
August 10, 2020, 10:07pm
#13
True what other ways are there?
nicemike40
(nicemike40)
August 10, 2020, 10:13pm
#14
Well, @xLawOut had one good way by splitting the string.
But you misunderstood me.
What do you want to do ? What is your high-level goal? Why would you want this function in the first place?
BanTech
(BanTech)
August 10, 2020, 10:18pm
#15
You’ll want to use ipairs
to guarantee the correct order every time.
And probably some preparations for non existent instances where this will error. Wrapping in a pcall or breaking if instance is nil may help.
There is also no need to set segments
to nil afterwards.
1 Like
thoraq
(thoraq)
August 10, 2020, 10:19pm
#16
I don’t really understand the question. Can’t you just do:
local var = game.Name
print(var)
happle5222
(DevHapple)
August 10, 2020, 10:20pm
#17
Well I am taking from a text box. (which is a string)
varjoy
(variable)
August 10, 2020, 10:32pm
#18
Well, actually, now I realize, this would be a great feature, example:
toobject(‘game.workspace’)
1 Like
And where does the text box get that string from? What if you went directly to the source and used something like a Remote/Bindable Event to get the same thing?
happle5222
(DevHapple)
August 10, 2020, 10:34pm
#20
So I am making a custom command line where someone inserts explorer.exe and it makes it visible (im working on a computer btw)
happle5222
(DevHapple)
August 10, 2020, 10:35pm
#21
The string comes from script.Parent.CommandLine