Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

help with enum?

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

✓ Accepted answer

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

3 comments

0

well now there is a new issue, edited question

0

The question has not been edited.

0

i didnt think you wouldreply so quickly, but its edited now

This discussion is preserved as it was. New answers, comments, and votes are closed.