How do I get the instances path as a string?

I’m trying to create a plugin that turns parts to scripts. In order to parent the part I need to get the path, but if the part’s name has a space then it breaks. How would I get the path to be game.Workspace[“PartsNameHere”]?

1 Like

Could you show an image of how you want it to look like. I don’t quite understand what you want. If you want to get an absolute path as a string, you can use :GetFullName.

1 Like

asdgqa

Your question isn’t exactly clear, or rather what you want to do isn’t. I can’t really understand what you’re doing and the example doesn’t fully help either.

GetFullName returns the full path to the instance as a string. I don’t quite understand why you can’t use that? Are you putting the return of GetFullPath into a string which is set as the script’s source? In that case, you could just break the string apart and if it has spaces, encapsulate it in brackets.

local part = workspace:FindFirstChild("Pa rt")

local partPath = part:GetFullName()
local appendStr = "game."

print(partPath) -- Workspace.Pa rt

-- Assuming you're sane and don't put periods in instance names
for _, token in ipairs(partPath:split(".")) do
    local appendToken = token

    if token:match(" ") then
        appendToken = "[\"" .. token .. "\"]"
    end

    appendStr = appendStr .. appendToken
end

print(appendStr) -- game.Workspace["Pa rt"]

Ok. I’m making a plugin that takes the selected parts and puts them into a script. So I’m trying to replicate each property and put it into the script. Except when I do Instance:GetFullName(), if the Instances name has a space in it, the script will break.

Since GetFullName just joins the names in the ancestry with a ., you can join them with "][" instead and sanitize the names to escape any \ or " in them.

local function GetFullName(x)
	local t = {}
	while x ~= game do
		local name = x.Name:gsub('[\"]', '\\%0')
		table.insert(t, 1, name)
		x = x.Parent
	end
	return 'game["'..table.concat(t, '"]["')..'"]'
end
14 Likes

thank you SO much for this i love you

why didnt the post’s maker put a solution mark on this?

1 Like