Skip to main content

TemplateProvider

Base of a template retrieval system. Templates can be retrieved from Roblox and then retrieved by name. If a folder is used all of their children are also included as templates, which allows for flexible organization by artists.

Additionally, you can provide template overrides as the last added template will always be used.

-- shared/CarTemplates.lua

return TemplateProvider.new(script.Name, script) -- Load locally
tip

If the TemplateProvider is initialized on the server, the the templates will be hidden from the client until the client requests them.

This prevents large amounts of templates from being rendered to the client, taking up memory on the client. This especially affects meshes, but can also affect sounds and other similar templates.

-- Server
local serviceBag = ServiceBag.new()
local templates = serviceBag:GetService(require("CarTemplates"))
serviceBag:Init()
serviceBag:Start()
-- Client
local serviceBag = ServiceBag.new()
local templates = serviceBag:GetService(require("CarTemplates"))
serviceBag:Init()
serviceBag:Start()

templates:PromiseCloneTemplate("CopCar"):Then(function(crate)
	print("Got crate!")
end)

Types

Template

type Template = Instance | Observable<Brio<Instance>> | table

Functions

new

TemplateProvider.new(
providerNamestring,
initialTemplatesTemplate
) → ()

Constructs a new TemplateProvider.

isTemplateProvider

TemplateProvider.isTemplateProvider(valueany) → boolean

Returns if the value is a template provider

Init

TemplateProvider:Init(serviceBagServiceBag) → ()

Initializes the container provider. Should be done via ServiceBag.

ObserveTemplate

TemplateProvider:ObserveTemplate(templateNamestring) → Observable<Instance>

Observes the given template by name

GetTemplate

TemplateProvider:GetTemplate(templateNamestring) → Instance?

Returns the raw template

PromiseCloneTemplate

TemplateProvider:PromiseCloneTemplate(templateNamestring) → Promise<Instance>

Promises to clone the template as soon as it exists

PromiseTemplate

TemplateProvider:PromiseTemplate(templateNamestring) → Promise<Instance>

Promise to resolve the raw template as soon as it exists

CloneTemplate

TemplateProvider:CloneTemplate(templateNamestring) → Instance?

Clones the template.

info

If the template name has a prefix of "Template" then it will remove it on the cloned instance.

AddTemplates

TemplateProvider:AddTemplates(containerTemplate) → MaidTask

Adds a new container to the provider for provision of assets. The initial container is considered a template. Additionally, we will include any children that are in a folder as a potential root

tip

The last template with a given name added will be considered the canonical template.

IsTemplateAvailable

TemplateProvider:IsTemplateAvailable(templateNamestring) → boolean

Returns whether or not a template is registered at the time

GetTemplateList

TemplateProvider:GetTemplateList() → {Instance}

Returns all current registered items.

GetContainerList

TemplateProvider:GetContainerList() → {Instance}

Gets all current the containers.

Destroy

TemplateProvider:Destroy() → ()

Cleans up the provider

Show raw api
{
    "functions": [
        {
            "name": "new",
            "desc": "Constructs a new [TemplateProvider].",
            "params": [
                {
                    "name": "providerName",
                    "desc": "",
                    "lua_type": "string"
                },
                {
                    "name": "initialTemplates",
                    "desc": "",
                    "lua_type": "Template"
                }
            ],
            "returns": [],
            "function_type": "static",
            "source": {
                "line": 84,
                "path": "src/templateprovider/src/Shared/TemplateProvider.lua"
            }
        },
        {
            "name": "isTemplateProvider",
            "desc": "Returns if the value is a template provider",
            "params": [
                {
                    "name": "value",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 104,
                "path": "src/templateprovider/src/Shared/TemplateProvider.lua"
            }
        },
        {
            "name": "Init",
            "desc": "Initializes the container provider. Should be done via [ServiceBag].",
            "params": [
                {
                    "name": "serviceBag",
                    "desc": "",
                    "lua_type": "ServiceBag"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 114,
                "path": "src/templateprovider/src/Shared/TemplateProvider.lua"
            }
        },
        {
            "name": "ObserveTemplate",
            "desc": "Observes the given template by name",
            "params": [
                {
                    "name": "templateName",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Observable<Instance>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 268,
                "path": "src/templateprovider/src/Shared/TemplateProvider.lua"
            }
        },
        {
            "name": "GetTemplate",
            "desc": "Returns the raw template",
            "params": [
                {
                    "name": "templateName",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Instance?"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 296,
                "path": "src/templateprovider/src/Shared/TemplateProvider.lua"
            }
        },
        {
            "name": "PromiseCloneTemplate",
            "desc": "Promises to clone the template as soon as it exists",
            "params": [
                {
                    "name": "templateName",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise<Instance>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 308,
                "path": "src/templateprovider/src/Shared/TemplateProvider.lua"
            }
        },
        {
            "name": "PromiseTemplate",
            "desc": "Promise to resolve the raw template as soon as it exists",
            "params": [
                {
                    "name": "templateName",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Promise<Instance>"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 323,
                "path": "src/templateprovider/src/Shared/TemplateProvider.lua"
            }
        },
        {
            "name": "CloneTemplate",
            "desc": "Clones the template.\n\n:::info\nIf the template name has a prefix of \"Template\" then it will remove it on the cloned instance.\n:::",
            "params": [
                {
                    "name": "templateName",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Instance?"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 453,
                "path": "src/templateprovider/src/Shared/TemplateProvider.lua"
            }
        },
        {
            "name": "AddTemplates",
            "desc": "Adds a new container to the provider for provision of assets. The initial container\nis considered a template. Additionally, we will include any children that are in a folder\nas a potential root\n\n:::tip\nThe last template with a given name added will be considered the canonical template.\n:::",
            "params": [
                {
                    "name": "container",
                    "desc": "",
                    "lua_type": "Template"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "MaidTask"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 484,
                "path": "src/templateprovider/src/Shared/TemplateProvider.lua"
            }
        },
        {
            "name": "IsTemplateAvailable",
            "desc": "Returns whether or not a template is registered at the time",
            "params": [
                {
                    "name": "templateName",
                    "desc": "",
                    "lua_type": "string"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 575,
                "path": "src/templateprovider/src/Shared/TemplateProvider.lua"
            }
        },
        {
            "name": "GetTemplateList",
            "desc": "Returns all current registered items.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "{ Instance }"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 586,
                "path": "src/templateprovider/src/Shared/TemplateProvider.lua"
            }
        },
        {
            "name": "GetContainerList",
            "desc": "Gets all current the containers.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "{ Instance }"
                }
            ],
            "function_type": "method",
            "source": {
                "line": 595,
                "path": "src/templateprovider/src/Shared/TemplateProvider.lua"
            }
        },
        {
            "name": "Destroy",
            "desc": "Cleans up the provider",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 626,
                "path": "src/templateprovider/src/Shared/TemplateProvider.lua"
            }
        }
    ],
    "properties": [],
    "types": [
        {
            "name": "Template",
            "desc": "",
            "lua_type": "Instance | Observable<Brio<Instance>> | table",
            "source": {
                "line": 77,
                "path": "src/templateprovider/src/Shared/TemplateProvider.lua"
            }
        }
    ],
    "name": "TemplateProvider",
    "desc": "Base of a template retrieval system. Templates can be retrieved from Roblox and then retrieved by name. If a folder is used\nall of their children are also included as templates, which allows for flexible organization by artists.\n\nAdditionally, you can provide template overrides as the last added template will always be used.\n\n```lua\n-- shared/CarTemplates.lua\n\nreturn TemplateProvider.new(script.Name, script) -- Load locally\n```\n\n:::tip\nIf the TemplateProvider is initialized on the server, the the templates will be hidden from the client until the\nclient requests them.\n\nThis prevents large amounts of templates from being rendered to the client, taking up memory on the client. This especially\naffects meshes, but can also affect sounds and other similar templates.\n:::\n\n```lua\n-- Server\nlocal serviceBag = ServiceBag.new()\nlocal templates = serviceBag:GetService(require(\"CarTemplates\"))\nserviceBag:Init()\nserviceBag:Start()\n```\n\n```lua\n-- Client\nlocal serviceBag = ServiceBag.new()\nlocal templates = serviceBag:GetService(require(\"CarTemplates\"))\nserviceBag:Init()\nserviceBag:Start()\n\ntemplates:PromiseCloneTemplate(\"CopCar\"):Then(function(crate)\n\tprint(\"Got crate!\")\nend)\n```",
    "source": {
        "line": 43,
        "path": "src/templateprovider/src/Shared/TemplateProvider.lua"
    }
}