Experimental

An early preview: docs, the API and the dashboard may change or break.

Report a problem
Module

@ember/items

Item stacks to give players and compare against what they hold.

local items = require("@ember/items")

Functions

items.create(id: string, changes: types.ItemChanges?) -> types.ItemStack
#

Makes a stack of the item, one unless changes sets a count.

local items = require("@ember/items")
local players = require("@ember/players")

players.joined:connect(function(player)
	-- One of an item: no changes needed.
	player.inventory:add(items.create("minecraft:wooden_sword"))

	-- A stack of 16, named and with lore. Name and lore are MiniMessage.
	local snacks = items.create("minecraft:cookie", {
		count = 16,
		name = "<gold>Starter Snacks",
		lore = { "<gray>Eat one when you're low.", "<dark_gray>Can't be sold." },
	})
	player.inventory:add(snacks)

	-- `data` rides along on the stack for scripts to read back later.
	local key = items.create("minecraft:tripwire_hook", {
		name = "<aqua>Vault Key",
		data = { vault = "north" },
	})
	player.inventory:set("hotbar", 9, key)
end)
items.deserialize(text: string) -> types.ItemStack?
#

Turns a string from items.serialize back into the stack, exactly as it was saved. Returns nil when its item no longer exists or is from a pack this experience doesn't use; raises when the string isn't a saved stack.

local items = require("@ember/items")
local players = require("@ember/players")

-- A serialized stack is a plain string, so it fits anywhere: a config table, a message, player data.
local prize = items.serialize(items.create("minecraft:diamond_sword", { name = "<aqua>Champion's Blade", glint = true }))

players.joined:connect(function(player)
	-- deserialize gives nil when the item no longer exists, so check before handing it out.
	local blade = items.deserialize(prize)
	if blade then
		player.inventory:add(blade)
	end
end)
items.full(id: string) -> types.ItemStack
#

Makes a full stack of the item: as many as one slot holds.

local items = require("@ember/items")
local players = require("@ember/players")

players.joined:connect(function(player)
	-- As many as a slot holds: 64 torches, 16 ender pearls, 1 sword.
	player.inventory:add(items.full("minecraft:torch"))
	player.inventory:add(items.full("minecraft:ender_pearl"))
end)
items.serialize(item: types.ItemStack) -> string
#

Turns a stack into a string to keep in player data or anywhere else a string goes; items.deserialize turns it back.

local data = require("@ember/data")
local items = require("@ember/items")
local players = require("@ember/players")
local types = require("@ember/types")

-- Each player's saved inventory: slot names to serialized stacks.
local profile = data.player({ inventory = {} :: { [string]: string } })

-- Put back what they had last time, then keep the saved copy up to date.
players.joined:connect(function(player)
	local inventory = player.inventory
	local slots: { [string]: types.ItemStack } = {}
	for slot, text in profile:get(player).inventory do
		-- nil when the item no longer exists, so a removed item leaves its slot empty.
		local item = items.deserialize(text)
		if item then
			slots[slot] = item
		end
	end
	inventory:restore(slots)

	-- `changed` fires at most once a tick, and the document saves as they leave.
	inventory.changed:connect(function()
		local saved = {}
		for slot, item in inventory:snapshot() do
			saved[slot] = items.serialize(item)
		end
		profile:get(player).inventory = saved
	end)
end)
items.tag(name: string) -> types.ItemType
#

The item tag with the name, with or without #; raises for an unknown tag.

local items = require("@ember/items")

-- An item tag's signals fire for every item in the tag.
local food = items.tag("#minecraft:meat")

food.consumed:connect(function(event)
	event.player:send(`<green>You ate {event.item.id}. Back in the fight!`)
	event.player:setHealth(event.player.maxHealth)
end)
items.type(id: string) -> types.ItemType
#

The item type with the id; typed handles come from @content and @pack/<name>. Raises for an unknown id.

local items = require("@ember/items")

-- Signals for one item type. Bare ids are this experience's own items from content.luau.
local dash = items.type("dash_feather")

-- Right-clicking with the feather launches the player the way they face.
dash.used:connect(function(event)
	local player = event.player
	-- Take one feather from the slot it was used from; stop if it's gone.
	if not event:spend() then
		return
	end
	local yaw = math.rad(player.look.yaw)
	local forward = vector.create(-math.sin(yaw), 0, math.cos(yaw))
	player:setVelocity(forward * 20 + vector.create(0, 8, 0))
end)