lets say i want to change a property called visible, but the only way i know i want to switch that is by a string that says “Visible”. can i do something like Frame[“Visible”] = true?
Yes, you can, using square brackets and a string is perfectly fine. As you can see in the pictures below, no error shows up, and it successfully renames.
Yes, you can index properties with a string in brackets as you showed.
ok and one more dumb question; can i get a part based on its path in a string? like “game.workspace.mypart” and i can get that part?
You can do something like workspace[“mypart”] and successfully index that part. OR you can set up a function that splits up the full name of the path, and finds the part.
You can do game[“workspace”][“mypart”]
every dot format can be converted to indexing something
(or yea, just do workspace[“mypart”])
ok but the issue is i need to be able to find thatpart form anywher in the game with my string path. like “game.workspace.dumbpartsfolder.dumbparts.mypart.partchild” or something (this is jsut an example)
Split up the string, and starting from workspace
, do the following workspace[StringPart[1]][StringPart[2]][StringPart[3]]
etc. Assuming the StringPart table exists with elements of strings.
ok but the path changes, so one time it might be “game.workspace.part” but another time it could be “game.replicatedstorage.parts.part”… so the amount of dots changes
Why would the ancestry of your part be changing so much while you didn’t even keep a reference to it?
Set a variable to the reference of the part and you don’t have to keep trying to get it over and over.
Sure, that’s why you can split up the full name (GetFullName()
) using string.split
, and use a for loop - this ensures it loops through each work, no matter how many there are.
do you really want a full explanation why i need to do this…
Yes because there is very likely better methods to go about it
Yes, theres no reason you should be changing its ancestry often without keeping a reference. You literally should keep a reference to it when you change its ancestry in the first place.
(If you simply misunderstood, say a part was in workspace. I define a variable as “Var1 = workspace.part”. Now, part is moved into, say, folder1, which is located inside workspace. Var1 will still have access to the part. You would not need to do “Var1 = workspace.folder1.part” to get the part again.)
A modulescript:
local function getInstance<T>(instance: Instance, ...: string): T
for _, childName in ipairs({ ... }) do
local child = instance:FindFirstChild(childName)
assert(child, string.format("%s is not a child of %s", childName, instance:GetFullName()))
instance = child
end
-- We want this function to be callable with generic types
return (instance :: any) :: T
end
return getInstance
A script using it:
local getInstance = require(script.Parent.getInstance)
local function getParentInstanceFromPath(splitPathNames)
if splitPathNames[1] == "game" then
table.remove(splitPathNames, 1)
end
local success, service = pcall(function()
-- GetService("workspace") annoyingly returns nil instead of erroring,
-- so we try it with capital first letter since all services have that
local serviceName = splitPathNames[1]:gsub("^%l", string.upper)
-- assert it exists just in case there's something else that causes
-- GetService to return nil instead of erroring
return assert(game:GetService(serviceName))
end)
if success then
table.remove(splitPathNames, 1)
return service, splitPathNames
end
error(`'{splitPathNames[1]}' is not a valid service name`)
end
local stringToParse = "game.Workspace.SpawnLocation.Decal"
local fullPath = stringToParse:split(".")
local parentInstance, remainingPath = getParentInstanceFromPath(fullPath)
local instance: Decal = getInstance(parentInstance, table.unpack(remainingPath))
print(instance:GetFullName()) --> Workspace.SpawnLocation.Decal
so im working on a assembly program for fun and one of the functions in it is “tog()”. the way you input this laungauge is through text, and one of the paremeters of “tog” is the path to a part. so “tog(game.workspace.part)” and of cours sometimes the tog function will be called for diffeent parts…
Why do you need the path? why not just the object itself?
because its a string, you cant put an object in a string
does this stil work even if the amount of parents changes?
Yeah it should, that point of the loop is to allow it to still work even when there is an ambiguous amount of ancestors