Roblox Studio - GUI 的按键切换

Roblox Studio - Key Toggle for GUI

好的,我目前正在 Roblox Studio 中制作一个项目。我有一个 Frame GUI,我想让它在玩家 运行 游戏时拥有 gui 的地方,他们可以按某个键来切换它 (ON) 和 (OFF)。所以一个 Open/Close 系统。我不想使用鼠标点击,而是想要一个按键切换。知道如何做到这一点吗?

http://wiki.roblox.com/index.php?title=Keyboard_input 这个link解释了2种方法,更好的方法是顶部或这里:

local toggle = false -- false is Off; true is On


function onKeyPress(actionName, userInputState, inputObject)
    if userInputState == Enum.UserInputState.Begin then
        print("R was pressed")
        if toggle == false then
             toggle = true
              -- INSERT Making GUI Visible
        else
            toggle = false
            -- INSERT making GUI Invisible
        end
    end
end

game.ContextActionService:BindAction("keyPress", onKeyPress, false, Enum.KeyCode.R)

-- 上面这行也可以写成:
-- game.ContextActionService:BindAction("keyPress", onKeyPress, false, "r")