Go to obejct by path in StringValue

Hey! I got a really simple question. I have stored a path in a StringValue, how can I go to the object by the StringValue? For example:

StringValue:
"game.Workspace.Folder.Part"

Another Script:

local part = StringValue.Value --get into "part" somehow
part.Transparency = 1

I think that you want to use ObjectValue and not StringValue because you are trying to reference a part.

ObjectValue | Roblox Creator Documentation

4 Likes
example[stringValue.Value].Transparency  = 1
4 Likes

Im not sure, but isnt loadstring used for that?

local part = loadstring(StringValue.Value)
part.Transparency = 1

im not sure ill try it and see what happens

2 Likes

Loadstring is for running a string as code.

loadstring(“print(‘hello’)”)()

1 Like

I’ll demonstrate for you below. Feel free to ask questions.

Script

To achieve the result you’re currently trying to obtain, you can set the string’s value to a variable.

local string_value = workspace.Part.StringValue.Value

We’re first setting a variable for the value held by the StringValue.
Then to find the Trail for your example, store it inside of ServerStorage. It doesn’t have to be inside ServerStorage, but in this example that’s where it’ll be held.

local trail = game:GetService(“ServerStorage”):FindFirstChild(string_value)

I’ll write a short conditional, that’ll let us know if we’ve found the trail or not.

if trail then
    print("Trail found: ".. trail)
else
    print("No trail fail!")
end

Final product

local string_value = workspace.Part.StringValue.Value
local trail = game:GetService("ServerStorage"):FindFirstChild(string_value)
if trail then
    print("Trail found: ".. trail)
else
    print("No trail fail!")
end

For future reference, you can use ObjectValue’s to directly reference objects.

Example

local trail = workspace.Part.ObjectValue.Value

This trail variable will let you directly store the Trail object within it.
The power of ObjectValue’s is now you can directly reference it, and do what you wish with the trail.

local trail = workspace.Part.ObjectValue.Value
trail.Parent = player.Character -- for example.

Just remember to always refer to the ObjectValue’s Value property, not just the ObjectValue instance.

2 Likes

So it works only on functions?
I thought it works like it turns string into lua

I’m not sure if it’s possible to get game from a string but it is possible to get everything under it.

"Workspace.Folder.Part"

local function FindObjectInPath(Path: string): Instance
	local CurrentThing = game
	for _, Target in ipairs(Path:split(".")) do
		CurrentThing = CurrentThing[Target]
	end
	return CurrentThing
end

local Object = FindObjectInPath("Workspace.Folder.Part")

print(Object) -- Part
4 Likes

Why would I need the trail? I mean, I’m kinda dumb at scripting… What’s even a trail? I’m trying to reach an object like if it was a normal variable, but by using what is written in the StringValue as a compass, I see no useful reason to use this “trail thing”

HAHA, trail was an example of something that can be held as one of the value’s. Trail, gun, sword, particle, I just chose something at random.

Sorry for the misunderstanding!

1 Like

Oh, haha! I was a bit confused, but now I got it. Thank you so much for your help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.