How to get object path using string

so i tried using this

local Path = "game.Workspace.Part1"
print(game.Parent:FindFirstChild(Path).BrickColor)

and as you guessed it didnt work.
i need help im dumb

You need to split the path around the dots and go down one level at a time.

how exactly im gonna do it?
i know only the way to

local Split = string.split(Path,".")

but idk whats next

You could use a loop for it like this:

local Path = "game.Workspace.Part1"
local Split = Path:split(".")
local Obj = if Path[1] == "workspace" then workspace else game
for i = 2, #split do
    Obj= Obj[Split[i]]
end
print(Obj, Obj:GetFullName())

If you do like an advanced function, you can use something like this:

function GetInstanceFromPath(Ancestor: (Instance | string)?, Path: string, PathSeparator: string?, FunctionTimeout: number?, WaitForCreation: boolean?, InstanceWaitTimeout: number?):(Instance?, string?)
	assert(typeof(Ancestor) == "Instance" or typeof(Ancestor) == "string" or Ancestor == nil, "Invalid Argument [1]; AncestorObject must be an instance.")
	assert(typeof(Path) == "string", "Invalid Argument [2]; String path must be provided.")
	assert(typeof(PathSeparator) == "string" or PathSeparator == nil, "Invalid Argument [3]; String expected.")
	assert(typeof(InstanceWaitTimeout) == "number" or InstanceWaitTimeout == nil, "Invalid Argument [4]; Number expected.")
	assert(typeof(FunctionTimeout) == "number" or FunctionTimeout == nil, "Invalid Argument [5]; Number expected.")
	--------------------------------------------------------------------------------------------------------------------------

	local PathSegments = string.split(Path, (PathSeparator or "\\"))
	local Thread = coroutine.running()
	local FoundInstance

	if typeof(Ancestor) == "Instance" then
		FoundInstance = Ancestor
	elseif typeof(Ancestor) == "string" then
		FoundInstance = game:GetService(Ancestor)
	elseif PathSegments[1] == "game" then
		FoundInstance = game:GetService(table.remove(PathSegments, 2))
		table.remove(PathSegments, 1)
	else
		FoundInstance = game:GetService(table.remove(PathSegments, 1))
	end

	coroutine.wrap(function()
		for _, Segment in ipairs(PathSegments) do
			if WaitForCreation and InstanceWaitTimeout then
				FoundInstance = FoundInstance:WaitForChild(Segment, InstanceWaitTimeout)
			elseif WaitForCreation then
				FoundInstance = FoundInstance:WaitForChild(Segment)
			else
				FoundInstance = FoundInstance:FindFirstChild(Segment)
			end
			if FoundInstance == nil then
				coroutine.resume(Thread, false, "Couldn't find the requested instance.")
				return
			end
		end
		coroutine.resume(Thread, true, FoundInstance)
	end)()

	if FunctionTimeout then
		task.delay(FunctionTimeout, function()
			coroutine.resume(Thread, false, "Timed out while waiting for the instance to be found.")
		end)
	end

	local Success, Response = coroutine.yield(Thread)
	if not Success then return nil, Response end
	return Response::any, nil::any
end


local Path = "Workspace.Part1.Part2"
warn(GetInstanceFromPath(nil, Path, "."))

sorry, but you’d need to define the ancestor of the instance you’re looking for, ima improve this to get it from the provided path automatically…

Edit: Updated the function to accept a full path without the need of a given ancestor.
Edit-2: Updated it to contain function timeout with coroutines implementation.

2 Likes

Alternatively, you could write something more straightforward, making sure you give the correct input types.

local function recurse(LastInstance, LastStringPart)
	local splitpart = LastStringPart:split(".")
	local newinst = LastInstance:FindFirstChild(splitpart[1])
	table.remove(splitpart,1)
	if #splitpart > 0 then
		return recurse(newinst, table.concat(splitpart,"."))
	else
		return newinst
	end
end
print(recurse(game,"Workspace.Baseplate").BrickColor)
1 Like

had something laying around in my files

local util = {}
function util:resolvePath(path)
	local resolve = function(current_path, current_in_path)
		if current_path == nil and current_in_path == 'game' then
			return game
		elseif current_path == game then
			return game:GetService(current_in_path)
		else
			if not current_path:FindFirstChild(current_in_path) then
				return nil
			else
				return current_path[current_in_path]
			end
		end
	end
	local current;
	for _, inside_path in pairs(path:split('.')) do
		current = resolve(current, inside_path)
		if current == nil then
			break
		end
	end
	return current
end

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