Skip to content

VibrationService ​

概览 ​

字段值
KindService · 服务 Service
Realmcommon

继承关系 ​

  • Unit(4 属性 / 25 函数 / 6 事件)
    • [VibrationService](1 函数)

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

VibrationService 是负责向指定玩家设备下发震动反馈的全局服务,核心能力是通过 StartVibration 一次性指定震动类型、震动次数与震动间隔,让目标玩家的设备按节奏产生震动。它把震动的下发集中到服务端触发,开发者无需自行处理客户端与设备之间的传递细节。

适用场景 ​

常用于服务端脚本中需要在关键时刻给玩家即时体感反馈的场合,例如玩家受到攻击、完成一次得分或进入倒数提示时,对特定玩家调用 StartVibration 触发震动。

使用要点 ​

先通过 game:GetService("VibrationService") 取得服务实例,再调用 StartVibration,把从 Players 服务或玩家相关事件中拿到的玩家对象作为首个参数传入,并依次给出震动类型、震动次数与震动间隔。整条调用链建议放在服务端脚本中执行,由服务端统一决定何时对哪名玩家下发震动。

注意事项 ​

StartVibration 应在服务端调用,传入目标 Player;客户端震动由内部事件转发到目标玩家设备,设备不支持震动时可能无效果。

代码示例 ​

玩家加入时触发设备震动 ​

lua
-- @runtime server
local Players = game:GetService("Players")
local VibrationService = game:GetService("VibrationService")

Players.PlayerAdded:Connect(function(player)
    VibrationService:StartVibration(player, 0, 2, 0.5)
end)

关联类型 ​

函数 (1) ​

StartVibration ​

签名:StartVibration(player: Player, vibrateType: Int, vibrateCount: Int, vibrateInterval: Float) -> void

向指定玩家发送手机震动指令。调用时依次传入玩家、震动类型、震动次数与震动间隔,四个参数均需提供,函数无返回值。

⚙ 调用环境:服务端。第一个参数必须是目标 Player,vibrateType 为震动模式,vibrateCount 为次数,vibrateInterval 为两次震动之间的间隔秒数。

参数类型说明
playerPlayer玩家
vibrateTypeInt震动模式
vibrateCountInt震动次数
vibrateIntervalFloat震动间隔

返回值 void

示例代码

服务端向指定玩家发送震动反馈

lua
-- @runtime server
local VibrationService = game:GetService("VibrationService")

local function notifyHit(player)
    VibrationService:StartVibration(player, 0, 2, 0.25)
end