Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Getting a number from alpha keys ( 1 - 9 )?

Is there any way I could get the actual number from a key pressed ( specifically a number key 1 - 9 )

So, when you press 1, it returns 1

I've tried KeyCode.Name, but that returns a written out version, "One".

function QuickSwitch(Key)
	local KeyName = Key.KeyCode.Name
	print(KeyName) --returns "One"
end

1 answer

✓ Accepted answer

Since its such a small set of numbers you could just create a dictionary and get the correct format from it, as follows:

local numbers = {["One"] = 1, ["Two"] = 2, ["Three"] = 3} ---etc.

function QuickSwitch(Key)
    local KeyName = numbers[Key.KeyCode.Name]
    print(numbers[Key.Name]) --returns "1"
end

I'm not sure if ive edited your script right to work how it should (line 4 might be wrong... line 5 should work as intended though :) )

3 comments

0

if you want more numbers (like... all the numbers..) leave a comment and I'll help you out :)

0

alright i changed the code a bit and using a pre-created dictionary kinda helped, my old method was returning nil from the array.

0

"returning nil from the array" if it was returning nil from the array I provided it may be because I capitalised the start of the numers because in your post you said it returned "One" not "one" so it could just be that simple :)

This discussion is preserved as it was. New answers, comments, and votes are closed.