How do I make gui turn off by pressing a key?

Im making a script for a Monitor, when the player presses the Key it should turn off.

local GUI = game.Workspace.Monitor.Screen.SurfaceGui
local uis = game:GetService("UserInputService")


game:GetService("UserInputService").InputBegan:connect(keyPressed)

function keyPressed(input)
	if input.KeyCode == Enum.KeyCode.F then
		GUI.Enabled = false
	end
end

My script doesn’t work though, can someone help me with this?

3 Likes

i don’t see a problem with your code so cant see why it would not work

do you get a error in the output window?

this is how i would write your script

local userInputService = game:GetService("UserInputService")
local surfaceGui = game.Workspace.Monitor.Screen.SurfaceGui

userInputService.InputBegan:connect(function(input, gameProcessed)
    if gameProcessed == true then return end
    if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
    if input.KeyCode ~= Enum.KeyCode.F then return end
    surfaceGui.Enabled = false
end)
2 Likes

I just flipped ur code into this… And it worked perfectly fine

function keyPressed(input)
	if input.KeyCode == Enum.KeyCode.F then
		print(1)
	end
end

game:GetService("UserInputService").InputBegan:Connect(keyPressed)
3 Likes

I tried both of your scripts, They don’t work for some reason.

I can’t figure out the problem, Maybe there is something wrong with the surfacegui?

2 Likes

Is your Enabled property ticked in the property window of the GUI?

2 Likes

Yes, From what I have seen it is enabled.

2 Likes

Try to disable it, and operate it only from script.

2 Likes

It is because you can’t pass arguments to local functions through connections.
Instead of making a new function named keyPressed, just do

uis.InputBegan:connect(function(Input,GameProcessed)
    -- GameProcessed is when the key is used for something else, like if the player is currently typing. (I think)
    if not GameProcessed and Input.KeyCode == Enum.KeyCode.F then
      GUI.Enabled = false
    end
end)
2 Likes

It doesn’t work for me, Do you think my script is in the wrong location?

3 Likes

It might be, is the script a LocalScript or a server script? Where is it placed?

2 Likes

Its a Server Script, placed in Workspace.

3 Likes

ServerScripts cannot pick up UserInputType events.
Instead, you can use a LocalScript and put it inside the StarterCharacterScripts or StarterPlayerScripts.
If you want the input to change the screen for all players, you could use a RemoteEvent so it’ll send to the server and then it’ll pass onto all other clients.

2 Likes

Okay, I placed it in StarterCharacterScripts, It works now.

Thanks.

3 Likes

No problem, if you have any other stuff you need help in don’t be afraid asking question here or looking up stuff :slight_smile:
The more you know after all

2 Likes

Yeah, Btw can I ask you a question?

2 Likes

For some reason the PMs just broke for me, I wanted to say ofc! Go ahead

1 Like

I worked on a script for a game I was working on a few months ago, The script was supposed to enable/disable a Model with one key, But it didn’t work and I had to use 2 keys (T, Y) to enable and disable it.

How do you disable/enable something with only one key?

1 Like

You mean to make it appear and disappear?
If so, it’s quite simple. Just use RemoteEvents.

LocalScript detects player input > Fire Server > Server picks up > Sanity check (Check if model exists and everything) > Do stuff

Example:

UserInputService.InputBegan:Connect(function(Input,GameProcessed)
    if not GameProcessed then
        local Key = Input.KeyCode
        if Key == Enum.KeyCode.T then
            EnableModel:FireServer(SelectedModel)
        elseif Key == Enum.KeyCode.Y then
            DisableModel:FireServer(SelectedModel)
        end
    end
end)

Server:

EnableModel.OnServerEvent:Connect(function(Model)
    if Model and Model:IsA("Model") then
        Model.Parent = -- Whatever parent you want it to be
    else
        warn("Model not found.")
    end
end)
1 Like

Yeah, Is there a way to do it with only one key instead of using 2 different keys?

Like using 1 key to enable/disable?

2 Likes

Ofc there is! Instead of having 2 RemoteEvents, you can have a single one, and the server checks if the model’s parent is the one you need it to be considered “Enabled”.

Example:

ToggleModel.OnServerEvent:Connect(function(Model)
    if Model and Model:IsA("Model)" then
        if Model.Parent ~= WantedParent then
            Model.Parent = WantedParent
        else
            Model.Parent = OtherParent
        end
    else
        warn("Model not found.")
    end
end)
2 Likes