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

KeyDown is not working correctly, how do i fix?

Here is what I got.

local HitAKey = game.Players.LocalPlayer:GetMouse().KeyDown
local Name = game.Players.LocalPlayer.Name

HitAKey:connect(function(key)
	if key == 'e' or 'E' then
		print(Name.." has pressed E!")
	elseif key == 'q' or 'Q' then
		print(Name.." has pressed Q!")
	end
end)

But when I press any keyboard button, like A, it prints "Player1 has pressed E!" But it still prints that if I press W, A, S, D keys again!

1 comment

0

this is NOT a request site

1 answer

✓ Accepted answer

use UserInputService

Make sure it's in a local script!

game:GetService("UserInputService").InputBegan:connect(function(input)
	local playerName = game.Players.LocalPlayer.Name

	if input.KeyCode == Enum.KeyCode.Q then

		print(playerName.."Has pressed Q")

	elseif input.KeyCode == Enum.KeyCode.E then

		print(playerName.."Has pressed E")

	end
end)

1 comment

0

How can I make it so it does something if u press 2 keys?

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