Roblox Studio - Key Toggle for GUI

喜夏-厌秋 提交于 2019-12-25 02:44:11

问题


Alright so I am currently making a project in Roblox Studio. And I have a Frame GUI, I want to make it where when a player has the gui when they're running the game, that they can press a certain key to toggle it (ON) and (OFF). So a Open/Close System. Instead of using a Mouse Click I want a key toggle. Any idea on how to do this?


回答1:


http://wiki.roblox.com/index.php?title=Keyboard_input This link explains 2 ways, the better way is the top or here:

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)

-- The above line could also been written as:
-- game.ContextActionService:BindAction("keyPress", onKeyPress, false, "r")



来源:https://stackoverflow.com/questions/50123221/roblox-studio-key-toggle-for-gui

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!