Changing Enum.KeyCode by string Value

  1. What do you want to achieve? Keep it simple and clear!
    So i’m working on keyAssign System and i need to change my OpenKey(Which include Enum.KeyCode…) with a string value like Enum.KeyCode.KeyCode
  2. What is the issue? Include screenshots / videos if possible!
    Idk if it’s possible bcz i can’ just change this
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Yes i did

Open script:

local function CtrlImput(input)
	OpenKey = Enum.KeyCode.KeyCode -- Here i need somehow convert string to KeyCode
	if input.KeyCode == OpenKey then
		if CtrlDebounce == false then
			gui.Visible = true
			CtrlDebounce = true
		else
			gui.Visible = false
			CtrlDebounce = false
		end
	end
end
UserInputService.InputBegan:Connect(CtrlImput)

And here is KeyAssign Function:

local function ChangeKeyBind()
	KeyBindButton.MouseButton1Click:Connect(function()
		KeyBindButton.Text = "Waiting for Input..."
		UserInputService.InputBegan:Connect(function(input)
			local KeyPressed = input.KeyCode
			KeyCode = KeyPressed
			KeyBindButton.TextScaled = "Change KeyBind... Current Keybind: "..KeyCode
		end)
	end)
end
3 Likes

The keycode returns either a number or an enum item. So no, this isn’t really possible using this way.

Ughh, i just wanna do keyAssign Function

You can actually do this by writing almost 100 lines of checking the text property changed

That literally won’t work and i don’t even know where you came up with this.

So as i understand key assign function in roblox is impossible?

It’s not impossible, you can make a dictionary list of every input with the key being the enum type and the value being the string.

But that’s inefficient because i haven’t fully understood your problem. Can you please elaborate more?

I have an TextButton when i click it or someone else, it waiting for Player click any button on Keyboard when he makes that OpenKey of gui becomes button which clicked Player. Example: OpenKey = B
Player Clicking text button and click button on keyboard…
Open Key = Q

Enum.KeyCode has predefined Names & Values.
Definition: Enum is a special type in the programming language where that type contains already predefined constants!

https://developer.roblox.com/en-us/api-reference/enum/KeyCode

A Keycode contains already predefined positions on your keyboard! I am quite unsure though, but I believe you can use the KeyCode Names as a string?

Try this and tell me how it goes:


local function GetKeyOnTextChanged()
	
	local KeyPressed
	UserInputService.InputBegan:Connect(function(input)
		
		KeyPressed = input.KeyCode
	end)
	
	return KeyPressed
end

I don’t really understand where i should put this code to make it work

Nvm, i found out you can use KeyCode.Name to return the string.

I don’t quite understand the example, but you can achieve the check of a keyboard by checking Enum.UserInputType.Keyboard and that will detect if a player interacted with any of the buttons.

https://developer.roblox.com/en-us/api-reference/enum/UserInputType

You can make it a standalone, im trying to fix some of the code cuz I didn’t put in valid espressions.

Wait you mean Enum.KeyCode.KeyCode.Name?

Exactly, im pretty sure this returns the name of the enum value as a string.

But i do not need an return of enum value. I need to change it with string value

Yeah, like where you see me put in KeyPressed = input.KeyCode, it would be KeyPressed = input.KeyCode.Name. Also just gonna say, I didn’t even know it existed till now

Ok that’s better but i need to use this in Enum.KeyCode

image