Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Searching your game for an object with a certain directory durring runtime?

Asked by 6 years ago
Edited 6 years ago

I am trying to take a string

local path = "game.Workspace.Part.Position.X"

and I need to print the Parts "X" Position with that string using that path, so something like this

local path = "game.Workspace.Part.Position.X"
print(path)

--Output (Normal)
--[[
game.Workspace.Part.Position.X
]]--

--Output (How I need it to be)
--[[
231 - (theoretical X position)
]]--

Is there anyway I can do this without recursively checking if every Object in my game has the same FullName as the path?

0
Use a for loop for every single part in the workspace and check if its the same or not Mr_Unlucky 1085 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago

This is actually pretty cool programming activity. The best way to do this would to be do some string manipulation. We basically treat every '.' as a divider between a parent and a child so we can get the name of something between each '.' and that will be the child we index from the parent we last indexed.

local path = "game.Workspace.Part.Position.X"
function proccessPath(s)
    local currentParent = game
    -- first thing we do is remove 'game.'
    local startNum= string.find(s,'.',1,true) -- need to tell the function to only search for strings not patterns because if set to false it will consider '.' as any character
    local currentString = string.sub(s,startNum + 1)
    local foundPeriod
    local foundChild
    repeat
        foundPeriod = false -- we can change this later instead of having an else statement to set it
        foundChild = false
        local startNum = string.find(currentString,'.',1,true)
        if startNum then
            foundPeriod = true
        elseif string.len(currentString) > 0 then
            foundPeriod = false
            startNum = 0
        else
            break
        end
        local childName = string.sub(currentString,1,startNum - 1) -- first we index from the start of the string to the position of the period to get the current child 
        currentString = string.sub(currentString,startNum + 1) -- then we save the new string which slices out the child to go on to the next one
        if typeof(currentParent) == "Instance" and currentParent:FindFirstChild(childName) or pcall(function() return currentParent[childName] end) then
            foundChild = true
            currentParent = currentParent[childName]
        end
    until not foundPeriod or not foundChild
    if foundChild then
        return currentParent -- returns part or property we got from the path name
    else
        warn("Child or property could not be found :(")
    end
end

print(proccessPath(path))

Outputs --> -6 Which was the position of the one part in my workspace at that time. The best thing about this is it works for properties too so you can do say "game.Workspace.Part.Position.X"

Well uh have fun with this function or whatever your gonna do with it. Cheers

0
Thank you so much! Le_JuiceBOX 54 — 6y
0
Don't use string.len(str). use #str User#24403 69 — 6y
Ad

Answer this question