:I'm trying to make a function that, when a player presses a certain key, it connects this remote event I already have set up. The remote event is functioning fine, but for some reason it gives me an error when I try to create the keybind function. I used one from the Roblox Developer wiki, and tweaked it a tiny bit to connect the keybind to this string value. What needs to be changed in my script?
function onKeyPress(inputObject, gameProcessedEvent) local value = script.Parent.ChordA.Value if inputObject.KeyCode == Enum.KeyCode(value) then print("ChordA Was Invoked") end end game:GetService("UserInputService").InputBegan:connect(onKeyPress)
Here's the error code it's giving me:
Workspace.NoteTrack.Keybinds.ConnectKeyBinds:2: attempt to call field 'KeyCode' (a userdata value) 18:59:19.223 - Stack Begin 18:59:19.225 - Script 'Workspace.NoteTrack.Keybinds.ConnectKeyBinds', Line 2 18:59:19.227 - Stack End
Hey if this hasn't been solved already I can show you a real easy way to do it
local value = "R" function onKeyPress(inputObject, gameProcessedEvent) if inputObject.KeyCode.Name == value then print("ChordA Was Invoked") end end game:GetService("UserInputService").InputBegan:connect(onKeyPress)
It's way easier to use the name and then just the key instead of referencing the Enum KeyCode
function onKeyPress(inputObject, gameProcessedEvent) local ChordAKey = Enum.KeyCode.R if inputObject.KeyCode == ChordAKey then print("ChordA Was Invoked") end end game:GetService("UserInputService").InputBegan:connect(onKeyPress)