Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to I make a key bind toggle for a GUI?

Asked by 3 years ago

I have a game stuck if first person, but I want you to be able to press c to toggle mouse free roaming or being stuck to the middle of the screen (Wouldn't be picky if you have to hold C instead of press C).

So here's the script that makes it free roam:

local plrGui = game.Players.LocalPlayer:WaitForChild("PlayerGui")
local screenGui = Instance.new("ScreenGui", plrGui)
local txtButton = Instance.new("TextButton")

txtButton.BackgroundTransparency = 1
txtButton.Size = UDim2.new(0, 0, 0, 0)

txtButton.Modal = true
txtButton.Parent = screenGui

The problem is that I can't figure out how to make it work only when C is pressed. I tried making a second script that disables it when C isn't pressed but it didn't appear to work.

I think the solution might be toggling if txtButton.Modal is equal to true or false, but I don't exactly know how to go about doing that.

Thanks for reading.

1 answer

Log in to vote
1
Answered by
Koriyoc 20
3 years ago

Have you ever used UserInputService? Well, if you haven't, UserInputService is a Service (obviously) that has events that can connect to when you press a key, such as InputBegan.

Then, when that event is fired, you can check what key to use by using InputBegan's parameters, input and gameProcessed. Check if input is C and then if it is, do the code that is needed.

0
ill try that out, thanks! PixelatedCube64 28 — 3y
0
I got it to work by watching this tutorial, recommend it for anyone having a problem close to mine. Thanks! PixelatedCube64 28 — 3y
Ad

Answer this question