Roblox中按下键盘‘E’键弹出和关闭GUI界面

我是一名新的Roblox开发者,认为这是学习制作游戏的好地方。今天在开发游戏时遇到了一个问题。我想在NPC旁按下‘E’键,我成功了。但唯一的问题是,我不知道如何在按下‘E’时让GUI界面出现和消失。以下是我的代码:

local HumanoidRootPart = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local UIS = game:GetService("UserInputService")
local Npc = game.Workspace.Noob.Head

UIS.InputBegan:connect(function(keyCode)
    if keyCode.keyCode == Enum.KeyCode.E then
        if (Npc.Position - HumanoidRootPart.Position).magnitude < 15 then
            game.StarterGui.TextF.TextLabel.Visible = 0
            wait(3)
            game.StarterGui.TextF.TextLabel.Visible = 1
        end
    end
end)

代码没有错误,按下‘E’的功能仍然正常,但GUI没有出现。我已经尝试将值设置为True或False。

点赞
用户13394423
用户13394423

我已经弄清楚了。如果有人看这个问题并且想知道,这个脚本几乎没问题。唯一的问题是你不能从启动 GUI 改变 GUI,你必须从 LocalPlayerGui 调用它,像这样:

local HumanoidRootPart = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local UIS = game:GetService("UserInputService")
local Npc = game.Workspace.Noob.Head

UIS.InputBegan:connect(function(keyCode)
    if keyCode.keyCode == Enum.KeyCode.E then
        if (Npc.Position - HumanoidRootPart.Position).magnitude < 15 then
            print("按下 E 键")
            game.Players.LocalPlayer.PlayerGui.TextF.TextLabel.Visible = true
            wait(3)
            game.Players.LocalPlayer.PlayerGui.TextF.TextLabel.Visible = false
        end
    end
end)
2020-04-23 23:07:19