Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ DOCUMENTATION
- = module(instance) [instance]
- --> existing instance id if it exists, or new instance id
- = module.new(instance) [instance]
- --> returns a new custom instance object or an existing one if one exists
- ~ .Unassociate() --> unassociate the tag with the instance
- ~ .ID --> get the instance's id
- = module.GetInstanceByID(id) [string]
- --> get an instance by id. errors if id doesn't exist.
- = module.UnregisterInstance(instance) [instance]
- --> unassociate a tag with an instance.
- = module.UnregisterID(id) [string]
- --> unassociate a tag with an instance
- ]]
- local HTTPService = game:GetService('HttpService')
- local module = {}
- module.__index = module
- local registeredInstances = {}
- setmetatable(module, {
- __call = function(class, instance)
- return class.new(instance).ID
- end,
- })
- function module.new(instance)
- if registeredInstances[instance] then
- return registeredInstances[instance]
- end
- local self = setmetatable({}, module)
- self:create(instance)
- return self
- end
- function module:create(instance)
- self.ID = HTTPService:GenerateGUID(false)
- registeredInstances[instance] = self
- function self.Unassociate()
- registeredInstances[instance] = nil
- end
- instance.AncestryChanged:Connect(function()
- if not instance:IsDescendantOf(game) or not instance:IsDescendantOf(nil) then
- module.UnregisterInstance(instance)
- end
- end)
- end
- function module.GetInstanceByID(ID)
- for instance, object in pairs(registeredInstances) do
- if object.ID == ID then
- return instance
- end
- end
- return error('No instance with id: ', ID)
- end
- function module.UnregisterInstance(instance)
- if registeredInstances[instance] then
- registeredInstances[instance] = nil
- return
- end
- return error('Instance doesn\'t exist: ', tostring(instance))
- end
- function module.UnregisterID(ID)
- for instance, object in pairs(registeredInstances) do
- if object.ID == ID then
- registeredInstances[instance] = nil
- return
- end
- end
- return error('ID doesn\'t exist: ', ID)
- end
- return module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement