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

What's the best way to detect a player pressing a NUMBER key?

Assume Mouse is already defined...

Mouse.KeyUp:connect(function(K)
if K=="1" then print("Pressed 1")
end

I'm actually not using this for printing an output but for a custom tool equip system. Sometimes my tool (or gun is this case) would glitch and once I unequip it(press 1) it would stay in my character and I'd have to press it again for it to go back to Backpack. Is there a more effective way for linking Number buttons?

1 answer

✓ Accepted answer

The UserInputerService handles keyboard input much more nicely than the KeyDown and KeyUp events.

local uis = game:GetService("UserInputService") --LocalScript

uis.InputBegan:connect(function(inst)
	if inst.KeyCode == Enum.KeyCode.One then
		print("Pressed 1")
	end
end)
uis.InputEnded:connect(function(inst)
	if inst.KeyCode == Enum.KeyCode.One then
		print("Released 1")
	end
end)

1 comment

0

I never thought about that! Thank you! You're a life saver.

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