ServiceProvider

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

无法创建
不可浏览

服务提供商是抽象类,存储并提供某些单例类别,取决于你使用其成员的继承类。

概要

方法

活动

属性

方法

FindService

写入并联

返回指定类别的服务,如果已创建,则返回无效名称的错误。

参数

className: string
默认值:""

返回

代码示例

ServiceProvider:FindService

print(game:FindService("Part"))
--> nil
print(game:FindService("Workspace"))
--> Workspace

GetService

返回带有请求的类名的服务。当用服务名称调用(例如 Debris)时,将返回该服务的实例。如果服务尚未存在,它将被创建,新服务返回。这是创建一些服务的唯一方法,也可用于具有不寻常名称的服务,例如RunService的名称是“运行服务”。

注意:

  • 此函数将返回 nil 如果 className 参数是一个现有类,但类不是服务。
  • 如果您尝试获取另一个对象下的服务,将被抛出一个错误,称“单例服务名称已存在”。

参数

className: string

请求服务的类名。

默认值:""

返回

请求的服务实例。

代码示例

ServiceProvider:GetService

local BadgeService = game:GetService("BadgeService")
local GameSettings = UserSettings():GetService("UserGameSettings")
print(BadgeService)
print(GameSettings)

活动

Close

当当前位置退出时发生火灾。


代码示例

This example prints "The place is closing" when the game.Close event fires.

ServiceProvider.Close

local function onClose()
print("The place is closing")
end
game.Close:Connect(onClose)

ServiceAdded

在服务创建时发射。

参数

service: Instance

ServiceRemoving

在服务即将被删除时发射。

参数

service: Instance