So this is one that I've been digging around trying to find a solid answer for a few days now with literal 0 success. I've only ever seen a couple discussions on it with nothing to show for it. I want to create a GUI that will allow the player to set an ability to specific keys. I can handle all the GUI scripting and all that but the main piece that is stumping me atm is having a way for a script to receive the keycode from something out of it.

game:GetService("UserInputService").InputBegan:Connect(function(key)
	if key.KeyCode == (set keycode here) then
		--Script functions
	end
end

I read somewhere that this can be achieved using StringValues or IntValues but it was never elaborated on and all my experiments have failed.

I've tried having a stringValue named SetKey, the same for an IntValue later, as a child of the script with the letter I want it set to in. For example, T, (116):

local setKey = script.SetKey.Value
if key.KeyCode == Enum.KeyCode.setKey then

I've tried having the full Enum in it, Enum.KeyCode.T which interestingly didn't error but also didn't function.

local setKey = script.SetKey.Value
if key.KeyCode == setKey then

And I've tried to look into string formatting but found myself only getting more confused every time I tried to figure it out. So, am I anywhere even remotely in the ball park for this one or am I'm about the distance of the sun levels of off? My guess is the latter.

I know I could achieve this using remote events and the like but that seems incredibly sloppy. Maybe I'm wrong and remote events are the way to go on achieving this one but I just wanna be doubly sure before I commit my previous ideas and attempts to the bin.

2 comments

1

to be compatible with your method: set the StringValue's value to be the keycode name. (Key.KeyCode.Name). then you can tag it as an enum once again by using Enum.KeyCode[StringValue.Value]

0

Ah ok. So I set the key to T and call it like that. Thank you. I'm still somewhat new to scripting so this may be stupid but I'm curious to know, why does it work Enum.KeyCode[setKey] but not Enum.KeyCode.setKey. Like, what's taking place that enables it?