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

How do I make a KeyDown script with UserInputService?

I'm not quite used to UserInputService, I've only been using the Mouse object. Can someone show me how to do something like this with UserInputService? Sorry if it sounds like a request, but I'm lost.

player = game.Players.LocalPlayer
mouse = player:GetMouse()

mouse.KeyDown:connect(function(key)
	if key == "f" then
		print("sup")
	end
end

1 answer

✓ Accepted answer

Due to KeyDown becoming deprecated, using UserInputService is the only way to get the press of a key. In order to use UserInputService, you must first use :GetService() and from there you can use the InputBegan event so that you can tell when the person gives any form of input. From here you can just use a simple function with parameters to figure out which key they pressed. You will need the KeyCode List in order to find the KeyCodes of keys that you don't know.

Please note that this is not a complete script but is merely the function that you would use to find a pressed key.

game:GetService("UserInputService").InputBegan:connect(function (input, _) -- recognizes input is being given
	if input.KeyCode == enum.KeyCode.W then -- checks to see if the key pressed was the W key
		print("the player pressed W")
	end
end)

2 comments

0

In line 2 it should be Enum.KeyCode.W not enum.KeyCode.W

0

Sorry my bad

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