Skip to content

Content

System · Content · server and client scripts

Content downloads published assets by id, spawns downloaded scenes, and looks up player avatars and headshots. An asset id is a decimal string that names a published scene, mesh, texture, or audio clip; for prefabs saved with the place, use Prefabs.

Fetch

Function
Fetch(asset: string, options: FetchOptions?) -> (ContentStatus, ContentAsset?)

Fetch downloads an asset into the memory cache, yielding until it completes or times out, and returns a ContentStatus and a ContentAsset on "Ok". Pass an optional FetchOptions table with Sha256 and Timeout.

Effects

  • Yields the calling thread until the download finishes or Timeout elapses.
  • Returns "Unavailable", nil when 64 requests are already pending or no content host is mounted.
  • The cache keeps up to 128 assets and 32 MiB of scenes, and drops the least recently used asset when full. A larger scene fails with "TooLarge".

Example

-- Server script.
local status, asset = Content.Fetch("1000000000000001", { Timeout = 10 })
if status ~= "Ok" or not asset then
    warn("Fetch failed: " .. status)
    return
end
if asset.Type == "Scene" then
    Content.Spawn(asset)
end

Spawn

FunctionServer only
Spawn(asset: ContentAsset, init: SpawnInit?) -> Entity?

Spawn is server-only. It creates the entity tree of a fetched scene, applies an optional SpawnInit table to the root, and returns the root entity. It returns nil when the asset was dropped from the cache or failed to instantiate.

Example

-- Server script.
local status, asset = Content.Fetch("1000000000000001")
if status == "Ok" and asset and asset.Type == "Scene" then
    local root = Content.Spawn(asset)
    if root then
        -- place the spawned scene 10 units up
        root.Position = Vector3.new(0, 10, 0)
    end
end
Errors (2)
Error When
Content.Spawn: asset <id> is a <type>; spawn only a Scene, and check asset.Type first asset.Type was not "Scene". Check asset.Type before calling Content.Spawn.
Spawn can only be called from the server Called from a client script.

Avatar

FunctionServer only
Avatar(player: Entity) -> (ContentStatus, AvatarManifest?)

Avatar looks up the avatar a player wears, yielding until the lookup finishes, and returns a ContentStatus and an AvatarManifest table with UserId, Body and BodyColors (keyed by "Head", "Torso", "LeftArm", "RightArm", "LeftLeg", and "RightLeg"), Face, Shirt, Pants, and up to 24 Accessories (Asset, Slot, Sha256).

Effects

  • Returns "NotFound", nil when the player has no platform account, or on a local playtest server when the player is not the signed-in editor.

Example

-- Server script.
local function socketFor(node: Entity, slot: AccessorySlot): Entity?
    for _, child in node:Children() do
        local attachment = child.Attachment
        local isRoot = attachment ~= nil and attachment.IsRoot
        if attachment and not isRoot and attachment.Slot == slot then
            return child
        end
        if not isRoot then
            local found = socketFor(child, slot)
            if found then
                return found
            end
        end
    end
    return nil
end

local function wear(character: Entity, asset: ContentAsset): Entity?
    local accessory = Content.Spawn(asset)
    if not accessory then
        return nil
    end
    local root = accessory.Attachment
    local socket = if root and root.IsRoot then socketFor(character, root.Slot) else nil
    if not socket then
        accessory:Destroy()
        return nil
    end
    local seatPosition = accessory.LocalPosition
    local seatRotation = accessory.LocalRotation
    accessory.Parent = socket
    accessory.LocalPosition = seatPosition
    accessory.LocalRotation = seatRotation
    accessory:Add(Component.Worn)
    return accessory
end

Component.User.Added:Connect(function(player: Entity)
    local status, manifest = Content.Avatar(player)
    if status ~= "Ok" or not manifest then
        return
    end
    for _, item in manifest.Accessories do
        local fetchStatus, asset = Content.Fetch(item.Asset, { Sha256 = item.Sha256 })
        local character = player.Avatar
        if fetchStatus == "Ok" and asset and character and asset.Type == "Scene" then
            wear(character, asset)
        end
    end
end)
Errors (1)
Error When
Avatar can only be called from the server Called from a client script.

Headshot

Function
Headshot(user: Entity | number) -> string?

Headshot returns the texture asset id of a user's headshot portrait, or nil when the user has no platform account. Pass a player entity or positive user id; the user does not need to be in the server.

Example

-- Server script.
local hud = World.Spawn({ UIRoot = true })
hud.Name = "HUD"
hud.Parent = World

local slots = 0
Component.User.Added:Connect(function(player: Entity)
    -- the label draws nothing until the portrait downloads
    local portraitId = Content.Headshot(player)
    local portrait = World.Spawn({
        Box = {
            Size = UIVector.fromPoints(96, 96),
            Position = UIVector.fromPoints(20 + slots * 104, 20),
        },
        Image = { Image = portraitId or "" },
    })
    portrait.Parent = hud
    slots += 1
end)

ReadData

Function
ReadData(path: string) -> buffer?

ReadData returns a buffer with the bytes of the file at path in the place's data/ folder, or nil when the place has no such file. path may start with data/ or leave it out.

Example

-- Server script.
local bytes = Content.ReadData("levels/1.bin")
if bytes then
    print(buffer.len(bytes), buffer.readu8(bytes, 0))
end
Errors (1)
Error When
invalid data file path path with its data/ prefix is shorter than 256 bytes, each part uses only ASCII letters, digits, _, - and ., parts are separated by /, and no part is empty, . or ...