I am changing the paradigm of my logic to adopt the logic of the ContextActionService.
In the example below, if the player presses the Z key for the first time, it will perform any action, for example opening a Gui. If Z is pressed again the action will be reversed, as in the example, it would close the previous Gui. In summary: Z = On, Z = Off.
local ContextActionService = game:GetService("ContextActionService")
local function TurnOff(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("It's OFF")
ContextActionService:UnbindAction("Off")
end
end
local function TurnOn(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("It's ON")
ContextActionService:BindAction("Off", TurnOff, false, Enum.KeyCode.Z)
end
end
ContextActionService:BindAction("On", TurnOn, false, Enum.KeyCode.Z)
Is this the correct approach?
2nd question
How can I adopt the above logic if I want to add a clickable button to perform the same function as the Z key?
That is, both Z and clicking a button will run the same workflow.