Skip to content

Instantly share code, notes, and snippets.

@howmanysmall
Created May 11, 2021 18:38
Show Gist options
  • Save howmanysmall/f0122c25e28446333563d770ecd08a34 to your computer and use it in GitHub Desktop.
Save howmanysmall/f0122c25e28446333563d770ecd08a34 to your computer and use it in GitHub Desktop.
replaces `getfenv(2).script` or whatever
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 GetObjectFromPathMetatable = {}
function GetObjectFromPathMetatable:__index(Path: string)
local CurrentObject = nil
for Index, PathValue in ipairs(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 GetObjectFromPathMetatable:__call(Path: string)
return self[Path]
end
local GetObjectFromPath = setmetatable({}, GetObjectFromPathMetatable)
print(GetObjectFromPath(debug.info(2, "s")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment