Problem with customizable controls

  1. What do you want to achieve? I want to create customizable controls

  2. What is the issue? I put in a key name and it doesn’t work

  3. What solutions have you tried so far? I tried looking up on YouTube and even the fourm but nothing works

Script(Located in StarterCharacterScripts):

local UIS = game:GetService("UserInputService")
local Key = game.StarterGui.ScreenGui.TextBox.Text

UIS.InputBegan:Connect(function(Input, IsTyping)
	if IsTyping then return end
	if Input.KeyCode == Key then
		print("Key Pressed!")
	end
end)

Key should be equal to Enum.KeyCode.LetterHere.

Here is updated code:

local UIS = game:GetService("UserInputService")
local Key = Enum.KeyCode.E --Or whatever you want.

UIS.InputBegan:Connect(function(Input, IsTyping)
	if IsTyping then return end
	if Input.KeyCode == Key then
		print("Key Pressed!")
	end
end)

If you want it to use the name of a KeyCode, you have to use if Input.KeyCode.Name == Key

Also, your code wouldn’t work anyways since you’re getting the Text of TextBox once, do this

local UIS = game:GetService("UserInputService")
local Key = game.Players.LocalPlayer.PlayerGui.ScreenGui.TextBox

UIS.InputBegan:Connect(function(Input, IsTyping)
	if IsTyping then return end
	if Input.KeyCode.Name == Key.Text then
		print("Key Pressed!")
	end
end)

Edit: Thank you @Jermartynojm for pointing out the flaw in my code, it has been fixed now

You said this:

Actually your code won’t work 100% good. Here’s what you should do in LocalScript:

local UIS = game:GetService("UserInputService")
local Key = game.Players.LocalPlayer.PlayerGui.ScreenGui.TextBox --I made change in this line.
UIS.InputBegan:Connect(function(Input, IsTyping)
	if IsTyping then return end
	if Input.KeyCode.Name == Key.Text then
		print("Key Pressed!")
	end
end)

Oh right, I didn’t realise that he referenced StarterGui, I thought he was referencing it correctly, my bad!

Alternatively, this solution is probably better

local UIS = game:GetService("UserInputService")
local Key = game.Players.LocalPlayer.PlayerGui.ScreenGui.TextBox
UIS.InputBegan:Connect(function(Input, IsTyping)
	if IsTyping then return end
	if Input.KeyCode == Enum.KeyCode[Key.Text] then --I changed stuff here.
		print("Key Pressed!")
	end
end)

It is more professional than the ones listed here

1 Like

Your code wouldn’t work eitherways because of

if Input.KeyCode == Enum.KeyCode[Key] then

Should be

if Input.KeyCode == Enum.KeyCode[Key.Text] then

Since doing your former code would try to find a keycode using a TextBox, not the text inside of it

1 Like

Missed that, updated my original post