在体验中定位朋友可能是个挑战。FriendsLocator 开发者模块 允许玩家在场景中轻松找到并传送到他们的朋友。
模块使用
安装
要在体验中使用 FriendsLocator 模块:
从 Studio 的 窗口 菜单或 主页 选项卡工具栏,打开 工具箱 并选择 创作者商店 选项卡。

确保选中 模型 排序,然后单击 查看所有 按钮以查看 类别。

找到并单击 Packages 瓦片。
找到 朋友定位器 模块并单击它,或将其拖放到 3D 视图中。

在 资源管理器 窗口中,将整个 FriendsLocator 模型移动到 ReplicatedStorage。在运行体验时,模块将开始运行。
在 Studio 中测试
要在 Studio 中测试模块,FriendsLocator 模块必须在多客户端仿真中运行,因为在单人游戏测试中不会出现朋友。
在 StarterPlayerScripts 中,创建一个新的 LocalScript 并将其重命名为 ConfigureFriendsLocator。

将以下代码粘贴到新的 ConfigureFriendsLocator 脚本中。 configure 函数中的 showAllPlayers 设置确保在 Studio 中测试时显示所有用户的定位器,而在已发布的场景中不会显示。
LocalScript - ConfigureFriendsLocatorlocal RunService = game:GetService("RunService")local ReplicatedStorage = game:GetService("ReplicatedStorage")local FriendsLocator = require(ReplicatedStorage.FriendsLocator)FriendsLocator.configure({showAllPlayers = RunService:IsStudio(), -- 允许在 Studio 中调试})从 Studio 的中层,启动一个 多客户端仿真,创建 2 个客户端。将打开三个新的 Studio 实例;一个模拟服务器和两个模拟客户端。

进入任一 客户端 Studio 实例,移动 100 个 studs 距离其他角色,您应该会看到一 个 定址图标 出现在其头顶。

连接到事件
FriendsLocator 模块公开了 事件,以便您在用户与定位器图标交互时引入自定义行为。
确保您已创建了在 Studio 中测试 中概述的 ConfigureFriendsLocator 脚本。
将第 8 行和第 11-13 行添加到脚本中:
LocalScript - ConfigureFriendsLocatorlocal RunService = game:GetService("RunService")local ReplicatedStorage = game:GetService("ReplicatedStorage")local FriendsLocator = require(ReplicatedStorage.FriendsLocator)FriendsLocator.configure({showAllPlayers = RunService:IsStudio(), -- 允许在 Studio 中调试teleportToFriend = false, -- 防止点击/轻触图标时传送})FriendsLocator.clicked:Connect(function(player, playerCFrame)print("您点击了角色的定位器图标", player.DisplayName)end)进行一次 多客户端测试,并点击另一个角色的定位器图标。请注意,您的角色不会传送到该位置,并且事件触发以允许自定义处理图标点击。
自定义定位器 UI
如果默认样式不适合您的体验,您可以用自己的 UI 替换默认的头像肖像 UI。

要替换默认 UI:
在 StarterGui 容器内创建一个新的 ScreenGui 实例。

作为新 ScreenGui 的子项创建一个名为 FriendLocator 的 Frame 实例,然后添加 ImageLabels、TextLabels 等元素来设计您的自定义 UI。

完成后,禁用 父 ScreenGui,以便在需要时模块才显示自定义定位器 UI。

(可选) 如果您希望朋友的头像肖像和 DisplayName 在自定义 UI 中显示,您可以将以下实例放置在 FriendLocator 框架内。
- 名称为 Portrait 的 ImageLabel。
- 名称为 DisplayName 的 TextLabel。

模块将查找这些项目,并分别显示朋友的头像肖像和/或显示名称。
API 参考
函数
configure
configure(config: table)
通过 config 表中的以下键/值覆盖默认配置选项。
| 键 | 描述 | 默认 |
|---|---|---|
| alwaysOnTop | 如果为 true,则在所有内容上方显示定位器图标,防止被 3D 世界对象遮挡。 | true |
| showAllPlayers | 如果为 true,则显示所有玩家的位置,而不仅仅是朋友;这可以帮助验证模块在 Studio 中的功能。 | false |
| teleportToFriend | 当点击或轻触朋友的定位器图标时,将玩家角色传送到朋友的位置。 | true |
| thresholdDistance | 定位器图标出现的摄像头距离阈值;距离此距离更近的朋友不会显示图标。 | 100 |
| maxLocators | 在任何给定时间显示的最大定位器图标数量。 | 10 |
LocalScript - ConfigureFriendsLocatorlocal ReplicatedStorage = game:GetService("ReplicatedStorage")local FriendsLocator = require(ReplicatedStorage.FriendsLocator)FriendsLocator.configure({alwaysOnTop = true,showAllPlayers = false,teleportToFriend = true,thresholdDistance = 100,maxLocators = 10})
事件
clicked
当本地玩家点击/激活定位器图标时触发。此事件只能在 LocalScript 中连接。
| 参数 | |
|---|---|
| player: Player | 定位器图标所属的玩家。 |
| playerCFrame: CFrame | 定位器图标所属玩家的 Humanoid.RootPart 的 CFrame。 |
LocalScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local FriendsLocator = require(ReplicatedStorage.FriendsLocator)
local localPlayer = Players.LocalPlayer
FriendsLocator.clicked:Connect(function(player, playerCFrame)
-- 在目标位置请求流
if Workspace.StreamingEnabled then
local success, errorMessage = pcall(function()
localPlayer:RequestStreamAroundAsync(playerCFrame.Position)
end)
if not success then
print(errorMessage)
end
end
print("您点击了角色的定位器图标", player.DisplayName, "位置为", playerCFrame.Position)
end)
visibilityChanged
当定位器图标在本地玩家的屏幕上显示/隐藏时触发。此事件只能在 LocalScript 中连接。
| 参数 | |
|---|---|
| player: Player | 定位器图标所属的 Player 对象。 |
| playerCFrame: CFrame | 定位器图标所属玩家的 Humanoid.RootPart 的 CFrame。 |
| isVisible: boolean | 定位器图标当前是否在本地玩家的屏幕上可见。请注意,如果 alwaysOnTop 为 false 且定位器在 3D 世界中的某个对象后面渲染,仍会为 true。 |
LocalScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local FriendsLocator = require(ReplicatedStorage.FriendsLocator)
FriendsLocator.visibilityChanged:Connect(function(player, playerCFrame, isVisible)
print("角色的定位器图标的可见性", player.DisplayName, ":", isVisible)
end)