I’m trying to make a path system like in windows file explorer. Like “C:/Users/…/”. This is my code.
local init = {}
function check_folders(start)
for _, v in pairs(start) do
return v
end
end
function init.run(orig_path)
if typeof(orig_path) ~= "string" then
warn("INIT: PATH TYPE IS NOT A STRING")
end
local path_new = string.lower(orig_path)
local path = string.split(path_new, "/")
if path[1] ~= 'CORE:' or path[1] ~= 'core:' then
warn("INIT: UNKNOWN PATH START")
end
for i, v in pairs(path) do
if not path[i-1]:FindFirstChild(path[i]) and check_folders(script.Parent.CORE) then
warn("INIT: PATH UNKNOWN")
else
return path
end
end
end
return init
use string.split() to get the different parts of the path, string.split("urmom/pro", "/") will split it using the slash character, if it can find any. So like damboi/pro will be split into splitresult[1] = damboi, splitresult[2] = pro
respectively.
When you use the script, I want you to be able to manipulate a folder via specifying a path with init.run. For example (refer to the imagine for the example), this:
For example, “CORE:/test”
I split the path into 2 arguments, CORE and test
I want to refer CORE to the head parent folder, and refer test to the test folder. refer to the image
You should probably end the code here if it isn’t a string.
At the first iteration, it would error because you cannot index 0 in any tables, and two you’re referring the string only, not the instance. You should’ve use FindFirstChild() on testing the presence of the folder in the explorer tab. Why are you subtracting the index to 1 anyway? You should be taking the second index which should be +1, same thing goes to the same index inside the FindFirstChild().
function init:run(path : string)
path = path:split(":/") -- "C:/memes" --> ["C", "memes"]
local parent = path[1]
local testParentPresent = workspace:FindFirstChild(parent, true) -- true means it will search for descendants with that name.
if testParentPresent then
print("Parent found!")
local testChildrenPresent = testParentPresent:FindFirstChild(path[-1])
if testChildrenPresent then
print("Child found!")
return testChildrenPresent
end
end
end)
Still works, I was struggling at that first, but I remember you can index an array backwards using negative numbers. So using path[-1], this will refer the last position in an array.