您可以创建一个普通脚本作为点击检测器的子级,并在其中执行以下操作:
-将点击检测器引用为script.Parent
- 创建一个.MouseClick 函数,该函数是启用 LocalScript 的代码
.MouseClick功能化妆:<Click Detector>.MouseClick:Connect(function(player) end)
<Click Detector>.MouseClick 函数带有一个参数,即点击按钮的玩家(在game.Players 中)。现在您需要引用玩家的PlayerGui,因为这是每个玩家的 Gui 所在的地方,也是唯一可以更改他们及其属性的地方。由于参数引用了game.Players 中的播放器,因此您只需执行<player>.PlayerGui 即可获取PlayerGui。 (如果您运行游戏并单击浏览器选项卡中Players 旁边的箭头,则会弹出玩家列表或仅您自己的列表。然后单击您的玩家旁边的箭头,您将看到PlayerGui。如果进一步,您将看到名为 Gui 的 ScreenGui,如果再次单击,您将看到名为 Frame 的 Frame。此框架内是我们将启用的 LocalScript。
Disabled 是脚本的布尔值(true 或 false)值属性。为了在 Lua 中引用属性,我们使用句点 .。一旦我们得到脚本,我们可以通过<LocalScript>.Disabled = false 来启用它。以下是您的代码结构应如下所示:
Define your Click Detector
Open a .MouseClick function with a parameter for the player who clicked
Get LocalScript by doing: local script = <player>.PlayerGui.Gui.Frame.LocalScript
Enable the script by doing: script.Disabled = false
end the .MouseClick function
更多帮助的链接:
https://developer.roblox.com/en-us/api-reference/class/ClickDetector
https://developer.roblox.com/en-us/api-reference/class/PlayerGui
已编辑:
这里应该是资源管理器中脚本的层次结构:
点击检测器
└ 脚本
现在您的代码将如下所示:
local ClickDetector = script.Parent --Reference your Click Detector for use in the script
ClickDetector.MouseClick:Connect(function(player) --'player' is the player who clicked it
local enableScript = player.PlayerGui.Gui.Frame.LocalScript --Reference your script through the game explorer by using dots '.'
enableScript.Disabled = false --Sets the 'Disabled' property to false (meaning it's now enabled)
end) --Close the .MouseClick function
如果您在对象引用方面需要帮助,我建议您观看 AlvinBlox 的视频:
https://www.youtube.com/watch?v=_K7stCkqFBY