Going insane with Roblox gsub please help

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

1 Like

There is one alternative:

  1. 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.

Thanks for the reply! Just tried your potential solution:
image
Still nothing! Haha! I’m going insane!!

Hey if you refresh I edited the code block with comments after the print statements to show what they return

Have you changed the '%p' by any chance and confirmed that it’s entirely the forward slash’s fault and not the punctuation?

Try changing it to something simple, such as a dot.

local replacePeriods = string.gsub(splitBeforeNodeName, '%.', '\47')
-- A dot is a magic character, so we must escape it using a percent sign

Also, it may be the current version of Studio you’re on right now, since my version works perfectly fine.

%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)

image
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.

Trying to turn this into a one liner:

local replacePeriods = string.gsub(string.split(string.split(nodePath, 'root')[2], nodeName)[1], '%.', '/')

Also gives me nothing, so its not some issue with assigning the split string to a variable

It was worth a shot I guess.

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

When trying your code, splitFromRoot is printed as:
example.lua
in console.
Also still nothing when printing replacePeriods

Can you try printing the other variables I’ve added?

print(replacementLimit)
print(replacedNodePath)
7
Players/Lozarth/PlayerGui/OS/root/shroomjak/example.lua

Okay, can you try printing this then? I think I’m starting to see an oddity. Now your split isn’t returning the proper value.

print(string.split(replacedNodePath, 'root'))
{Players/Lozarth/PlayerGui/OS/, example.lua}

Yeah okay string.split is having a stroke.

1 Like

bro this is literally me rn
IIyoiBnK_200x200