How do I check if a path is valid?

Hello, I am using a module script to clone objects found in ReplicatedFolder and in a given parent if I wanted, where the objects all have unique names.

Currently I can check if I am specifying a parent or if I have any arguments at all. The issue is that I do not know how to return an error if the parent does not exist.

For example, if I do…

print(objectHandler:Get('Tool', foo):GetFullName())

and it would only print out Tool with no error whereas doing…

print(objectHandler:Get('Tool', workspacee):GetFullName())

prints out Workspace.Tool.

I’ve considered using FindFirstChild but each time I call this, the parent of the Child could vary, like Workspace and Player.Backpack.

Any help is appreciated.

Here is my code so far:

local objectHandler = {}

local scope = game:GetService('ReplicatedStorage')

function objectHandler:Get(objectName_, newParent_)
	if objectName_ then
		local object = scope:FindFirstChild(objectName_, true)
		
		if object then
			local newObject = object:Clone()
			
			if newParent_ then --check validity of parent somewhere inside here
				newObject.Parent = newParent_
				
				return newObject
			else
				return newObject
			end
		else
			return error('CANNOT FIND [ ' .. objectName_ .. ' ] IN [ ' .. tostring(scope) .. ' ]')
		end
	else
		return error('NO ARGUMENTS FOUND')
	end
end

return objectHandler

I’d use :GetDescendants() to find the newParent_ then check if descendant.Name == newParent_ and parent it if yes.

The issue with this is that newParent_ is an object, so it would essentially have to exist first, and that isn’t my goal.

The part where it says if newParent_ then only checks if there the argument exists.

I supposed I could use game:GetDescendants but the issue is that the object could have many other objects with the same name.

The only objects that have unique names are the ones in scope. (not to mention that using game:GetDescendants()) error out

‘The current identity (2) cannot Class security check (lacking permission 6)’

Well then in your parameters, define newParent_ by doing like

game.Workspace.foo

It can be like that and it would probably work.

I am not try to eliminate the error, I am trying to catch it so I can correctly error it out.

Certain situations would call for parenting objects to the player’s Gui, exchanging leaderstats, giving players certain tools, etc.

Yea but to do that, you’d need the playerName atleast so you can access his player gui and set it as the parent.

print(objectHandler:Get('Tool', player.PlayerGui.Test):GetFullName())