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

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

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

✓ Accepted answer

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.

2 comments

0

ill try that out, thanks!

0

I got it to work by watching this tutorial, recommend it for anyone having a problem close to mine. Thanks!

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