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.
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:
d.Pet
, and d["Pet"]
. Likewise, you can "index" an Enum in two ways:
Enum.KeyCode.E
, andEnum.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 code
hold 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