Skip to content

CommodityService ​

概览 ​

字段值
KindService · 服务 Service
Realmcommon

继承关系 ​

  • Unit(5 属性 / 28 函数 / 6 事件)
    • [CommodityService](17 函数 / 5 事件)

> Service 继承 Unit 仅表示运行时类型关系;作者可用 API 以当前 Service 页面显式列出的成员为准,不自动包含 Unit 的创建、层级或销毁能力。

商业化服务,提供玩家商品与权益查询、商品面板交互、免费商品请求、消耗操作以及相关状态事件。

适用场景 ​

适合服务端校验玩家商品、粉丝团或会员权益,以及客户端展示商品详情、购买面板并监听商业化状态变化。

使用要点 ​

先通过 game:GetService("CommodityService") 获取服务。商品、权益与数量等可信查询应在服务端执行;面板展示和交互请求按方法运行端约束调用,商品 ID 必须来自项目实际配置。

注意事项 ​

CommodityService 同时包含客户端 UI/请求方法与服务端查询方法。HasCommodity/GetCommodityCount/IsAuthorFans/GetFanClubLevel/IsFansRightsActive/IsVip/IsVipRightsActive/GetGoods/GetGoodsInfo 等查询在客户端会返回默认值或无法获得真实业务数据,应在服务端脚本中调用;ShowGoodsDetailPanel/ShowGoodsPurchasePanel/SetGoodsPanelVisible/SetGoodsVisible/RequestFreeGoods/ConsumeCommodity 可按具体交互从客户端或服务端触发。

代码示例 ​

监听道具消耗事件 ​

lua
-- @runtime client
local commodityService = game:GetService('CommodityService')
commodityService.CommodityConsumed:Once(function(commodityId, consumeNum, consumePlayer)
    print('消耗道具:', commodityId, '数量:', consumeNum, '玩家:', consumePlayer)
end)

关联类型 ​

事件 (5) ​

CommodityConsumed ​

签名:CommodityConsumed(commodityId: Int, consumeNum: Int, consumePlayer: Player) 触发:local

CommodityService 的 CommodityConsumed 事件在玩家消耗道具时触发,回调依次提供道具 ID(commodityId)、消耗数量(consumeNum)和关联玩家(consumePlayer)。

回调参数类型说明
commodityIdInt道具Id
consumeNumInt数量
consumePlayerPlayer玩家

示例代码

监听一次道具消耗事件

lua
-- @runtime client
local commodityService = game:GetService("CommodityService")
commodityService.CommodityConsumed:Once(function(commodityId, consumeNum, consumePlayer)
    print("道具消耗:", commodityId, "数量:", consumeNum)
    if consumePlayer ~= nil then
        print("消耗玩家:", consumePlayer:GetName())
    end
end)

CommodityObtain ​

签名:CommodityObtain(commodityId: Int, consumeNum: Int, consumePlayer: Player, isBringInto: Bool) 触发:local

CommodityService 的 CommodityObtain 事件在玩家获得道具时触发,回调依次提供道具 ID(commodityId)、数量(consumeNum)、关联玩家(consumePlayer)以及布尔标记 isBringInto。

回调参数类型说明
commodityIdInt道具Id
consumeNumInt数量
consumePlayerPlayer玩家
isBringIntoBool是否为跨局携带道具

示例代码

监听商品获得事件

lua
-- @runtime client
local commodityService = game:GetService('CommodityService')
commodityService.CommodityObtain:Once(function(commodityId, consumeNum, consumePlayer, isBringInto)
    print('获得商品:', commodityId, '数量:', consumeNum)
    print('玩家:', consumePlayer, '是否带入:', isBringInto)
end)

GoodsPurchaseCompleted ​

签名:GoodsPurchaseCompleted(goodsId: String, goodsNum: Int, player: Player) 触发:local

CommodityService 的 GoodsPurchaseCompleted 事件在商品购买完成后触发,回调提供商品 ID(goodsId,字符串)、商品数量(goodsNum)和玩家(player)。

回调参数类型说明
goodsIdString商品Id
goodsNumInt数量
playerPlayer玩家

示例代码

监听商品购买完成

lua
-- @runtime client
local commodityService = game:GetService('CommodityService')
commodityService.GoodsPurchaseCompleted:Once(function(goodsId, goodsNum, player)
    print('购买完成:', goodsId, '数量:', goodsNum, '玩家:', player)
end)

FanClubStatusChanged ​

签名:FanClubStatusChanged(player: Player, oldLevel: Int, newLevel: Int) 触发:local

CommodityService 的 FanClubStatusChanged 事件在玩家的粉丝团等级发生变化时触发,回调提供玩家(player)、旧等级(oldLevel)和新等级(newLevel)。

回调参数类型说明
playerPlayer玩家
oldLevelInt变更前等级(0=未加入)
newLevelInt变更后等级(0=已退出)

示例代码

监听一次粉丝团等级变化

lua
-- @runtime client
local commodityService = game:GetService("CommodityService")
commodityService.FanClubStatusChanged:Once(function(player, oldLevel, newLevel)
    local playerName = player ~= nil and player:GetName() or "未知玩家"
    print(string.format("粉丝团等级变化:%s 从 %d 级变为 %d 级", playerName, oldLevel, newLevel))
end)

VipStatusChanged ​

签名:VipStatusChanged(player: Player, isVip: Bool) 触发:local

CommodityService 的 VipStatusChanged 事件在玩家的乐园会员状态发生变化时触发,回调提供玩家(player)以及当前的会员状态(isVip)。

回调参数类型说明
playerPlayer玩家
isVipBool变更后的会员状态

示例代码

监听乐园会员状态变化事件

lua
-- @runtime client
-- 获取商品服务与本地玩家
local commodityService = game:GetService("CommodityService")
local players = game:GetService("Players")
local localPlayer = players.LocalPlayer
if localPlayer == nil then return end
-- 单次监听乐园会员状态变化:isVip 表示变化后的会员状态
commodityService.VipStatusChanged:Once(function(player, isVip)
    print("会员状态变化", isVip)
    print("变化玩家是本地玩家", player == localPlayer)
end)
print("已注册会员状态变化监听")

函数 (17) ​

HasCommodity ​

签名:HasCommodity(player: Player, commodityId: Int) -> Bool (是否拥有道具)

查询指定玩家是否拥有某个道具,返回 Bool 类型的结果。

参数类型说明
playerPlayer玩家对象
commodityIdInt道具Id

返回值 Bool (是否拥有道具)

示例代码

在服务端检查玩家是否拥有道具

lua
-- @runtime server
local commodityService = game:GetService("CommodityService")
local players = game:GetService("Players")
local player = players:GetPlayers()[1]

if player ~= nil then
    local hasCommodity = commodityService:HasCommodity(player, 10001)
    print("玩家是否拥有道具:", hasCommodity)
end

GetCommodityCount ​

签名:GetCommodityCount(player: Player, commodityId: Int) -> Int (拥有道具数量)

查询指定玩家拥有某个道具的数量,返回 Int 类型的数量值。

参数类型说明
playerPlayer玩家对象
commodityIdInt道具Id

返回值 Int (拥有道具数量)

示例代码

在服务端查询玩家道具数量

lua
-- @runtime server
local commodityService = game:GetService("CommodityService")
local players = game:GetService("Players")
local player = players:GetPlayers()[1]

if player ~= nil then
    local count = commodityService:GetCommodityCount(player, 10001)
    print("玩家拥有道具数量:", count)
end

ConsumeCommodity ​

签名:ConsumeCommodity(player: Player, commodityId: Int, num: Int) -> void

消耗指定玩家一定数量的指定道具,调用时需传入玩家、道具 ID 和消耗数量。

参数类型说明
playerPlayer玩家对象
commodityIdInt道具Id
numInt消耗数量

返回值 void

示例代码

消耗项目配置中的道具

lua
-- @runtime client
local commodityService = game:GetService("CommodityService")

local function consumeConfiguredCommodity(player, commodityId, amount)
    if player == nil or commodityId == nil or amount == nil or amount <= 0 then return end
    commodityService:ConsumeCommodity(player, commodityId, amount)
end

-- 调用方应传入项目商业化配置中的真实 commodityId 和消耗数量

SetGoodsPanelVisible ​

签名:SetGoodsPanelVisible(player: Player, visible: Bool) -> void

设置指定玩家的付费道具商店面板可见性,调用时需传入玩家和布尔可见状态。

参数类型说明
playerPlayer玩家对象
visibleBool可见性

返回值 void

示例代码

设置付费道具商店可见性

lua
-- @runtime client
-- 获取商品服务与本地玩家
local commodityService = game:GetService("CommodityService")
local players = game:GetService("Players")
local localPlayer = players.LocalPlayer
if localPlayer == nil then return end
-- 控制付费道具商店面板显示
local visible = true
commodityService:SetGoodsPanelVisible(localPlayer, visible)
print("已设置付费道具商店可见性", visible)

ShowGoodsDetailPanel ​

签名:ShowGoodsDetailPanel(player: Player, rawGoodsId: String) -> void

为指定玩家显示某个商品的详情界面,调用时需传入玩家和商品原始 ID(rawGoodsId)。

参数类型说明
playerPlayer玩家对象
rawGoodsIdString商品Id

返回值 void

示例代码

用项目配置的商品 ID 打开详情

lua
-- @runtime client
local commodityService = game:GetService("CommodityService")

local function showGoodsDetail(player, rawGoodsId)
    if player == nil or rawGoodsId == nil or rawGoodsId == "" then return end
    commodityService:ShowGoodsDetailPanel(player, rawGoodsId)
end

-- 调用方应把商业化后台配置的真实 rawGoodsId 传给 showGoodsDetail

ShowGoodsPurchasePanel ​

签名:ShowGoodsPurchasePanel(player: Player, rawGoodsId: String, showTime: Float) -> void

为指定玩家显示某个商品的购买界面,调用时需传入玩家、商品原始 ID 和 showTime 参数。

参数类型说明
playerPlayer玩家对象
rawGoodsIdString商品Id
showTimeFloat展示时间

返回值 void

示例代码

用项目配置的商品 ID 打开购买面板

lua
-- @runtime client
local commodityService = game:GetService("CommodityService")

local function showGoodsPurchase(player, rawGoodsId, displaySeconds)
    if player == nil or rawGoodsId == nil or rawGoodsId == "" then return end
    commodityService:ShowGoodsPurchasePanel(player, rawGoodsId, displaySeconds)
end

-- 调用方应传入商业化后台配置的真实 rawGoodsId 与展示时长

SetGoodsVisible ​

签名:SetGoodsVisible(player: Player, rawGoodsId: String, visible: Bool) -> void

设置指定玩家处某个付费商品的可见性,调用时需传入玩家、商品原始 ID 和布尔可见状态。

参数类型说明
playerPlayer玩家对象
rawGoodsIdString商品Id
visibleBool可见性

返回值 void

示例代码

按项目配置切换商品可见性

lua
-- @runtime client
local commodityService = game:GetService("CommodityService")

local function setConfiguredGoodsVisible(player, rawGoodsId, visible)
    if player == nil or rawGoodsId == nil or rawGoodsId == "" then return end
    commodityService:SetGoodsVisible(player, rawGoodsId, visible)
end

-- 调用方应把商业化后台配置的真实 rawGoodsId 传入该函数

RequestFreeGoods ​

签名:RequestFreeGoods(player: Player, rawGoodsId: String) -> void

为指定玩家请求领取免费商品,调用时需传入玩家和商品原始 ID(rawGoodsId)。

参数类型说明
playerPlayer玩家对象
rawGoodsIdString商品Id

返回值 void

示例代码

请求项目中已配置的免费商品

lua
-- @runtime client
local commodityService = game:GetService("CommodityService")

local function requestConfiguredFreeGoods(player, rawGoodsId)
    if player == nil or rawGoodsId == nil or rawGoodsId == "" then return end
    commodityService:RequestFreeGoods(player, rawGoodsId)
end

-- 仅传入商业化后台中已配置为免费商品的真实 rawGoodsId

IsAuthorFans ​

签名:IsAuthorFans(player: Player) -> Bool (是否为粉丝)

查询指定玩家是否为当前地图作者的粉丝,返回 Bool 类型结果。

参数类型说明
playerPlayer玩家对象

返回值 Bool (是否为粉丝)

示例代码

在服务端检查作者粉丝状态

lua
-- @runtime server
local commodityService = game:GetService("CommodityService")
local players = game:GetService("Players")
local player = players:GetPlayers()[1]

if player ~= nil then
    local isFans = commodityService:IsAuthorFans(player)
    print("玩家是否为作者粉丝:", isFans)
end

GetFanClubLevel ​

签名:GetFanClubLevel(player: Player) -> Int (粉丝团等级(0=未加入,1/2/3=各级粉丝团))

获取指定玩家对当前地图创作者的粉丝团等级,返回 Int 类型等级值。

参数类型说明
playerPlayer玩家对象

返回值 Int (粉丝团等级(0=未加入,1/2/3=各级粉丝团))

示例代码

在服务端获取粉丝团等级

lua
-- @runtime server
local commodityService = game:GetService("CommodityService")
local players = game:GetService("Players")
local player = players:GetPlayers()[1]

if player ~= nil then
    local level = commodityService:GetFanClubLevel(player)
    print("玩家粉丝团等级:", level)
end

IsFansRightsActive ​

签名:IsFansRightsActive() -> Bool (是否开启)

查询当前地图是否开启了粉丝团权益功能。

返回值 Bool (是否开启)

示例代码

在服务端检查粉丝团权益开关

lua
-- @runtime server
local commodityService = game:GetService("CommodityService")
local isActive = commodityService:IsFansRightsActive()
print("粉丝团权益是否开启:", isActive)

IsVip ​

签名:IsVip(player: Player) -> Bool (是否为乐园会员)

查询指定玩家是否为乐园会员,返回 Bool 类型结果。

参数类型说明
playerPlayer玩家对象

返回值 Bool (是否为乐园会员)

示例代码

在服务端检查乐园会员状态

lua
-- @runtime server
local commodityService = game:GetService("CommodityService")
local players = game:GetService("Players")
local player = players:GetPlayers()[1]

if player ~= nil then
    local isVip = commodityService:IsVip(player)
    print("玩家是否为乐园会员:", isVip)
end

IsVipRightsActive ​

签名:IsVipRightsActive() -> Bool (是否开启)

查询当前地图是否开启了乐园会员权益功能。

返回值 Bool (是否开启)

示例代码

在服务端检查乐园会员权益开关

lua
-- @runtime server
local commodityService = game:GetService("CommodityService")
local isActive = commodityService:IsVipRightsActive()
print("乐园会员权益是否开启:", isActive)

PromptFanClubPurchase ​

签名:PromptFanClubPurchase(player: Player) -> void

向指定玩家发起加入或升级粉丝团的提示操作。

参数类型说明
playerPlayer玩家对象

返回值 void

示例代码

在服务端提示玩家加入粉丝团

lua
-- @runtime server
local commodityService = game:GetService("CommodityService")
local players = game:GetService("Players")
local player = players:GetPlayers()[1]

if player ~= nil and commodityService:IsFansRightsActive() then
    commodityService:PromptFanClubPurchase(player)
    print("已向玩家弹出粉丝团购买界面")
end

PromptVipPurchase ​

签名:PromptVipPurchase(player: Player) -> void

向指定玩家发起开通乐园会员的提示操作。

参数类型说明
playerPlayer玩家对象

返回值 void

示例代码

在服务端提示玩家开通乐园会员

lua
-- @runtime server
local commodityService = game:GetService("CommodityService")
local players = game:GetService("Players")
local player = players:GetPlayers()[1]

if player ~= nil and commodityService:IsVipRightsActive() then
    commodityService:PromptVipPurchase(player)
    print("已向玩家弹出乐园会员购买界面")
end

GetGoods ​

签名:GetGoods() -> Array<Any> (商品信息列表)

获取当前地图所有商品信息的列表。

返回值 Array<Any> (商品信息列表)

示例代码

在服务端获取商品列表

lua
-- @runtime server
local commodityService = game:GetService("CommodityService")
local goodsList = commodityService:GetGoods()

for index, goodsInfo in ipairs(goodsList) do
    print("商品", index, tostring(goodsInfo))
end

GetGoodsInfo ​

签名:GetGoodsInfo(goodsId: String) -> Map (商品信息,不存在时返回nil)

获取指定商品的详细信息。

参数类型说明
goodsIdString商品Id

返回值 Map (商品信息,不存在时返回nil)

示例代码

在服务端获取指定商品信息

lua
-- @runtime server
local commodityService = game:GetService("CommodityService")
local goodsId = "10001"
local goodsInfo = commodityService:GetGoodsInfo(goodsId)

if goodsInfo ~= nil then
    print("商品信息:", tostring(goodsInfo))
else
    print("未找到该商品")
end