I have the following code which takes an Instance and converts it’s path to a Unix file system path.
In my code, “nodes” are either a stringValue instance (for files) or a folder instance (for folders obviously). Just to clarify any confusion with what the purpose of my code is
function module.getNodePath(node: node)
local nodeName = node.Name
local nodePath = node:GetFullName()
print(nodePath) --Players.Lozarth.PlayerGui.OS.root.home.shroomjak.example.lua
local splitAfterRoot = string.split(nodePath, 'root')[2]
print(splitAfterRoot) --.home.shroomjak.example.lua
local splitBeforeNodeName = string.split(splitAfterRoot, nodeName)[1]
print(splitBeforeNodeName) --.home.shroomjak.
local replacePeriods = string.gsub(splitBeforeNodeName, '%p', '/')
print(replacePeriods) -- NOTHING
local path = replacePeriods .. nodeName
print(path) --example.lua
return path
end
For example, this code will take the following path: Players.Lozarth.PlayerGui.OS.root.home.shroomjak.example.lua
And is supposed to convert it to /home/shroomjak/example.lua
and return it as a string.
My issue is with gsub on the replacePeriods variable local replacePeriods = string.gsub(splitBeforeNodeName, '%p', '/')
Returns: NOTHING
For some reason I can replace the periods with LITERALLY ANYTHING EXCEPT FOR A FORWARD SLASH. When I use a forward slash and print the result, I get NOTHING. This is driving me insane and I don’t know why its doing this. I can do double forward slashes:
local replacePeriods = string.gsub(splitBeforeNodeName, '%p', '//')
Returns: //home//shroomjak//example.lua
This issue ONLY happens with forward slashes, someone please help before I start pulling my hair out. Thanks in advance! :3
Use the character codepoint of the Slash (Solidus) symbol. You can look up Unicode Characters in this Wikipedia Article Of Unicodes. It uses code 47 or x2F. To use a codepoint, you must put a backslash and then the number.
local replacePeriods = string.gsub(splitBeforeNodeName, '%p', '\47') -- Uses the 47th character in the Unicode Standard, or the Slash symbol.
Can you print out all the different stages of the string manipulation process, and tell us what they return? Use your example path please for ease of debugging on our end.
Edit: To clarify, using a normal forward slash does work for me, so it may be an issue in the pipeline here.
Edit2: I’m blind as a bat and I missed the fact you already did, I apologize, let me look over it and debug.
%p is similar to %. except that it matches all punctuation including periods (?, !, etc.) and I just tried this, still nothing. (Edit: I will try updating my studio)
I’m sorry man, I… literally cannot replicate your results at all, so I’m not sure how to go about assisting you… You might try only doing your gsub AFTER you concat path, you can limit the replacements with this:
string.gsub(path, '%p', '/', #string.split(path, ".") - 2) -- - 2 should make it ignore the last period, if it doesn't, use 3 or 4 (I'm unsure, but I believe 2 or 3 works for your path)
Using replacement limit preserves the period for the filename.
Okay it seems to be some extremely strange issue with trying to gsub a string that has been split. If I do: string.gsub(nodePath, '%.', '/')
I get: Players/Lozarth/PlayerGui/OS/root/home/shroomjak/example/lua
Attempting to do this same thing on splitAfterRoot gives me: lua
Attempting to do the same thing on splitBeforeNodeName gives me: nothing
Even after trying to do tostring on both splitAfterRoot and splitBeforeNodeName I get the same two things. Extremely weird.
Another alternative, but counter-intuitive and definitely a bad solution so far, is to put the string in a string.gmatch() loop, and concatenate forward slashes from there.
local replacePeriods = "/" -- Guaranteed that a slash always appears at the start.
for objectName in string.gmatch(splitBeforeNodeName, '%P+') do -- Any string that's NOT a punctuation
replacePeriods ..= objectName .. "/"
end
Try this, please, and get back to me on the result of splitFromRoot
function module.getNodePath(node: node)
local nodeName = node.Name
local nodePath = node:GetFullName()
local replacementLimit = #(string.split(nodePath, ".")) - (2 + if string.sub(nodePath,1,1) == "." or string.sub(nodePath,-1,-1) == "." then 1 else 0)
local replacedNodePath = string.gsub(nodePath, '%p', '/', replacementLimit)
local splitFromRoot = string.split(replacedNodePath, 'root')[2]
print(splitFromRoot) -- Replace this with result, let me know if it works.
print(nodePath) --Players.Lozarth.PlayerGui.OS.root.home.shroomjak.example.lua
local splitAfterRoot = string.split(nodePath, 'root')[2]
print(splitAfterRoot) --.home.shroomjak.example.lua
local splitBeforeNodeName = string.split(splitAfterRoot, nodeName)[1]
print(splitBeforeNodeName) --.home.shroomjak.
local replacePeriods = string.gsub(splitBeforeNodeName, '%p', '/')
print(replacePeriods) -- NOTHING
local path = replacePeriods .. nodeName
print(path) --example.lua
return path
end
local replacePeriods = "/" -- Guaranteed that a slash always appears at the start.
for objectName in string.gmatch(splitBeforeNodeName, '%P+') do -- Any string that's NOT a punctuation
replacePeriods ..= objectName .. "/"
end
print(replacePeriods) -- Nothing
local path = replacePeriods .. nodeName
return path
My Roblox studio has to be cursed with demons that are messing up my code because it still doesn’t work