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

What's wrong with this keybind function?

Asked by 6 years ago

: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

0
What is ChordA's value? Rare_tendo 3000 — 6y
0
I set the value to one capital letter, just as it would be in the script. EnderGamer358 79 — 6y

2 answers

Log in to vote
0
Answered by
Vulkarin 581 Moderation Voter
6 years ago

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

0
._. this actually worked, I tweaked it a bit to connect to a string value. Thanks a lot! EnderGamer358 79 — 6y
0
no problem Vulkarin 581 — 6y
Ad
Log in to vote
0
Answered by
hellmatic 1523 Moderation Voter
6 years ago
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)
0
This would work, but I want it to be easily changed by external scripts. That was the whole point of the string value to begin with. I'll try tweaking this and see if it works EnderGamer358 79 — 6y

Answer this question