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

This is a strange bug with UserInputService, is it not?

UserInputService is messy. Everything is defined. The only thing that doesn't go according to plan is that the ToggleEquipSword function doesn't trigger when I press One, nor does it notify me in the output that I pressed a key. It only prints the type of user input.

userInput.InputBegan:connect(
	function(input)
		print(input.UserInputType)
		if input.UserInputType == 'Keyboard' then
			print('Keyboard') --To let me know that I've pressed a key
			if input.KeyCode == Enum.KeyCode.One then
				print'1'
				ToggleEquipSword()
			end
		end
	end
)

Strangely enough, even though the UserInputType is Keyboard, It doesn't print 'Keyboard' to the output as intended

2 comments

0

Change line 6 to 'if input == Enum.KeyCode.One then'.

0

That's not helpful for explaining. Even though your solution is correct. I prefer Taslem's answer.

1 answer

✓ Accepted answer

input.UserInputType isn't a string -- it's an enum. (You can print it out to check this)

Things of different types will not usually be equal.


Enums represent "enumerated" values.

This is meant to solve a problem related to what you're doing.

What happens if I say part.TopSurface = "Blorp"? "Blorp" isn't one of the surface types. Enums are a solution where you explicitly state all of the values that are acceptable: Enum.SurfaceType. has them all. (Of course, ROBLOX lets you use strings usually for assignment).



local KeyboardEnum = Enum.UserInputType.Keyboard

.....

	if input.UserInputType == KeyboardEnum then

1 comment

1

Oh, only for assignment. I see. Thank you, you've been of great help, Portal guy.

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