Is there a way to unstring a string?

(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.

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”.

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.

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

Well you can do something similar with loadstring

print(loadstring("return game.Name")())

Sorry if I misunderstood

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

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.

True what other ways are there?

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?

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

I don’t really understand the question. Can’t you just do:

local var = game.Name
print(var)

Well I am taking from a text box. (which is a string)

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?

So I am making a custom command line where someone inserts explorer.exe and it makes it visible (im working on a computer btw)

The string comes from script.Parent.CommandLine