Help needed with Roblox 'KeyCode' function

Hi, I made a KeyCode function and I tried to make it open a GUI, but it failed several times. I’m a beginner scripter so do not know too much yet. :upside_down_face: GuiScript - Roblox Studio 2019-12-31 7_19_53 PM (2)

1 Like

KeyCode is uppercase:

keycode.KeyCode

and Magnitude should be uppercase:

Magnitude

Edit: You should have the script inside the starterGui. More specifically, in the GUI that is being opened,

2 Likes

I don’t think it is the capitals I tested with strings at first, and it worked.

your script is getting a bit confused when your parameter is keyCode too.

try this

uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
--code
end
end)
``

Not working maybe it’s where I put my localscript? Should I put it in Starter GUI or inside the ScreenGUI?

Alright, haven’t tested it yet but try this:

local U = game:GetService("UserInputService")
Player = game.Players.LocalPlayer

U.InputBegan:connect(function(input, gEP)
if input.KeyCode == Enum.KeyCode.E then 
	game.StarterGui.ScreenGui.Enabled = true
	
end
end)

Should go in StarterGui.

1 Like

That has no meaning. I did however finally figure out the issue. You are trying to get at game.StarterGui, it should be player(The real player not the character).PlayerGui.

it is super confusing that your player is your humanoidRootPart lol.

I will rewrite your code.

local UIS = game:GetService("UserInputService")
local PressE = workspace.PressE
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local root = character:FindFirstChild("HumanoidRootPart")

UIS.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	if input.KeyCode == Enum.KeyCode.E then
		if (PressE.Position - root.Position).Magnitude < 12 then
			player.PlayerGui.ScreenGui.Enabled = true
		end
	end
end)


if you do put the local script inside of the screenGui, you could just do script.Parent.Enabled = true, which should simplify things-

4 Likes
local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then
        --Put code here.
    end
end)

That would be the little template for when players press “e”.

1 Like

I believe that including gameProcessedEvent should be included in this template.

1 Like

I used that exact code, but for when the player presses “m”, and it still worked for me.

1 Like

First thing first, in future copy your code instead of posting a picture and paste into this:
```
– Code Here

```

Heres and equivalent piece of code to the ones above but it uses ContextActionService instead

-- Local Script in StarterPlayerScripts
local player = game.Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
    character = player.CharacterAdded:wait()
end
local HRP = character.HumanoidRootpart
local humanoid = character:WaitForChild("Humanoid")
local contextActionService = game:GetService("ContextActionService")

local PressE = game.workspace.PressE

local function openGui(actionName, inputState, inputObject)
	if inputState == Enum.UserInputState.Begin then
		if (PressE.Position - HRP.Position).magnitude < 12 then
			if player.PlayerGui.ScreenGui.Enabled then
				player.PlayerGui.ScreenGui.Enabled = false
			else
				player.PlayerGui.ScreenGui.Enabled = true
			end
		end
	elseif inputState == Enum.UserInputState.End then
		-- Unneccesary but leaving it to show how ContextActionService works
	end
end
 
contextActionService:BindAction("openGui", openGui, false, Enum.KeyCode.E)
1 Like

When I use your code, this appears in the output. Players.MegaTheMagnificent.PlayerGui.ScreenGui.GuiScript:9: attempt to index upvalue ‘root’ (a nil value)

Idk what you’re referring to when you say “exact code”, however I believe you have the wrong perceiption of what gameProcessedEvent is. GPE basically ensures that the function will only trigger when you’re not typing in a gui for example the chat.

It is working completely fine for me, do you mind pasting the script you have now?

local PressE = game.Workspace.PressE
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character
local root = character:FindFirstChild("HumanoidRootPart")

UIS.InputBegan:Connect(function(input, gpe)
if input.KeyCode == Enum.KeyCode.E then
if (PressE.Position - root.Position).Magnitude < 12 then
player.PlayerGui.ScreenGui.Enabled = true
end
end
end)

Neat, though a few things seems unnecessary. Why include the variable “humanoid” when it is not in use, secondly… doing local character = player.Character or player.CharacterAdded:Wait() is already enough, there is no point in doing an if statement?

2 Likes

Try :WaitForChild() instead of :FindFirstChild() but other than that it should be working completely fine. Where have you put the script?

1 Like

I put it in the StarterGui, also it’s a localscript if that helps, lol.

Nvm it works, tysm for the help!

1 Like

I usually just copy paste those local script declarations and humanoid is often important so I have it at the tope of every local script even if I don’t use it. And with that “local character = player.Character or player.CharacterAdded:Wait().” Thank you! I’ve been using that stupid looking if statement for so long and your method is much nicer!

1 Like