Skip to content

Instantly share code, notes, and snippets.

@howmanysmall
Created October 16, 2022 21:49
Show Gist options
  • Save howmanysmall/48fda51b347347601e842ff953856305 to your computer and use it in GitHub Desktop.
Save howmanysmall/48fda51b347347601e842ff953856305 to your computer and use it in GitHub Desktop.
local function GetService(ServiceName: string)
return game:GetService(ServiceName)
end
local ServicesMetatable = {}
function ServicesMetatable:__index(Index)
local Success, Object = pcall(GetService, Index)
local Service = Success and Object
self[Index] = Service
return Service
end
local Services = setmetatable({}, ServicesMetatable)
local Metatable = {}
function Metatable:__index(Path: string): Instance?
local CurrentObject = nil
for Index, PathValue in string.split(Path, ".") do
if Index == 1 then
local Service = Services[PathValue]
if not Service then
self[Path] = nil
return nil
end
CurrentObject = Service
else
if CurrentObject then
local Object = CurrentObject:FindFirstChild(PathValue)
if Object then
CurrentObject = Object
end
end
end
end
self[Path] = CurrentObject
return CurrentObject
end
function Metatable:__call(Path: string): Instance?
return self[Path]
end
local GetObjectFromPath = setmetatable({}, Metatable)
return GetObjectFromPath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment