0
votes

I'm a new developer to roblox and figured it would be a good place to start learning to make games. I was working on my game today and I ran into an issue. I wanted to press 'E' while by a NPC and I got that to work. The only problem was, I have no clue how to make the GUI appear and Dissapear when I press E. This is what I have written,

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)

it comes out with no error. The code to press E still works, but the GUI does not pop up. I have tried setting the values to True or False

1

1 Answers

0
votes

I've Figured it out. if anyone is looking at this question and wondering, this script is almost ok. The only problem is that you cannot change a GUI from the starter GUI, you have to call it from LocalPlayerGui Like this:

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("Key E was pressed")
            game.Players.LocalPlayer.PlayerGui.TextF.TextLabel.Visible = true
            wait(3)
            game.Players.LocalPlayer.PlayerGui.TextF.TextLabel.Visible = false
        end
    end
end)