Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 9 years ago

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

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

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)
0
I never thought about that! Thank you! You're a life saver. Devotional 210 — 9y
Ad

Answer this question