一个容器对象,用于保存玩家的道具。玩家的背包中的任何 Tool 都会显示在他们屏幕底部的库存中从库存中选择 Tools 将装备 Tool ,将其从背包移至玩家角色。
背包也可以存储 Scripts 和 LocalScripts ,当放置在玩家的背包中时运行。
当玩家的角色生成时,其 StarterPack 和 StarterGear 的内容将复制到他们的背包中。一旦角色死亡,背包将被移除,新的背包将被创建--用 StarterPack 和 StarterGear 的内容填充它。
Roblox 提供一个接口,让玩家默认在屏幕底部访问背包和库存。如果开发者希望禁用默认 Roblox 背包 GUI 并替换为自拥有的,他们可以使用 StarterGui:SetCoreGuiEnabled() 来执行此操作。
背包可以从客户端和服务器访问。
local Players = game:GetService("Players")-- 从服务器脚本访问背包:local backpack = Players.PlayerName.Backpack-- 从本地脚本访问背包:local backpack = Players.LocalPlayer.Backpack
代码示例
This sample includes a simple function demonstrating how a Tool can be given to a Player by parenting it to their Backpack.
Backpack Give Tool
local Players = game:GetService("Players")
local function giveTool(player, tool)
local backpack = player:FindFirstChildOfClass("Backpack")
if backpack then
tool.Parent = backpack
end
end
local function onPlayerAdded(player)
local tool = Instance.new("Tool")
giveTool(player, tool)
end
Players.PlayerAdded:Connect(onPlayerAdded)