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

help with enum?

Asked by
theCJarmy7 1293 Moderation Voter
8 years ago
local code = game.Players.LocalPlayer.codes.inventory.Value

function onKeyPress(inputObject, gameProcessedEvent)
    if gameProcessedEvent then return end

if inputObject.KeyCode == Enum.KeyCode[code] then
if script.Parent.Visible == true then
    script.Parent.Visible = false
    else script.Parent.Visible = true
        end
    end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

this is supposed to let the player choose multiple keycodes for opening their inventory. i only have the guis ready for q and i, but i ran into this problem. it doesnt update the variable when i change the source of the variable. basically, it would change the variable if i put the entire script inside a while loop, but the problem is that with the wait in the while loop, i would have a limited amount of time to press the key.

1 answer

Log in to vote
3
Answered by
XAXA 1569 Moderation Voter
8 years ago

ORIGINAL PROBLEM: given a variable "code" which stores a string, Enum.KeyCode.code will not work properly.

Given a dictionary d = { ["Pet"] = "Dog" }, you can index Pet to get the string "Dog" in two ways:

  1. d.Pet, and
  2. d["Pet"].

Likewise, you can "index" an Enum in two ways:

  1. Enum.KeyCode.E, and
  2. Enum.KeyCode["E"].

Considering that code is a string, you should index it the second way, like so:

if inputObject.KeyCode == Enum.KeyCode[code] then

EDITED PROBLEM: When the user edits game.Players.LocalPlayer.codes.inventory, the KeyCode does not change along with it.

The problem is that code holds a string, not the StringValue. When the user edits the StringValue, the string stored in code does not change with it. To fix this, you should make codehold the StringValue instead (in this case, it's game.Players.LocalPlayer.codes.inventory).

Line 1 should be:

local code = game.Players.LocalPlayer.codes.inventory

Since we made code store the StringValue itself instead of the string, we have to fix line 6, like so:

if inputObject.KeyCode == Enum.KeyCode[code.Value] then
0
well now there is a new issue, edited question theCJarmy7 1293 — 8y
0
The question has not been edited. XAXA 1569 — 8y
0
i didnt think you wouldreply so quickly, but its edited now theCJarmy7 1293 — 8y
Ad

Answer this question