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?
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)
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)
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.
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?
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)
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)