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

How would i set the Input.KeyCode's value to a StringValue's value?

Asked by
oSyM8V3N 429 Moderation Voter
6 years ago

I have a function that chose's a random key from a table containing keys like : Q, E, R, T, Y, etc. Then i made this other function using UIS:

UIS.InputBegan:Connect(function(Input, gameProcessedEvent)
    local KeyCode = Input.KeyCode
    local keyPress = sgui:FindFirstChild("Menu").status.KPositioner.keyValue.Value
    if not gameProcessedEvent then

        if KeyCode == Enum.KeyCode.keyPress.Value then
            print("WEWE")
        end
    end
end)

But when i wrote if KeyCode == Enum.KeyCode.keyPress.Value then, it outputs an error. Since the keyPress's Value is a random key from the table, how would i make it so the KeyCode depends on the keyPress's Value?

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

You should know that there are two index operators (which is basically what you do with Enums). The two operators are . and []. Using the dot, it's always going to look with something with that name. Using the brackets, you can input a string or a variable to look for something inside. To easily fix this you do

if KeyCode == Enum.KeyCode[keyPress] then
    print("WEWE")
end
0
It still shows the Players.YBN_oSy.PlayerScripts.keydownthing:110: bad argument #2 to '?' (string expected, got nil) error oSyM8V3N 429 — 6y
0
if KeyCode == Enum.KeyCode[tostring(keyPress)] then decla123 45 — 6y
Ad

Answer this question