Essentially, I'm trying to make a script, where the player presses the key 'E', then the brick color changes. However, this does not work. Any explanation or help would be appreciated.
(For more information, this is a normal script within the brick. Do I have to put the script in workspace? or can I keep it within the brick?)
local debounce = false local userinputservice = game:GetService("UserInputService") userinputservice.InputBegan:Connect(function(input, gameProcessedEvent) if input.UserInputType == Enum.UserInputType.Keyboard then if input.KeyCode == Enum.KeyCode.E then if not debounce then debounce = true script.Parent.BrickColor = BrickColor.new("Really red") debounce = false end end end end)
Hi!
Your code is completely fine, just the implementation of it is the issue. Since you're using the UserInputService service, you need to use the service in a localscript (as highlighted in the docs)
Which ultimately means you must use a localscript within these locations:
(provided from the docs)
So, when I run this localscript inside of StarterPlayerScripts
local debounce = false local userinputservice = game:GetService("UserInputService") userinputservice.InputBegan:Connect(function(input, gameProcessedEvent) if input.UserInputType == Enum.UserInputType.Keyboard then if input.KeyCode == Enum.KeyCode.E then if not debounce then debounce = true print('My script works!') debounce = false end end end end)
.. I get this result in the output:
My script works! - Client - LocalScript:10
Showing that the code above works completely fine.
The UserInputService with keycodes only works in local scripts, try putting a localscript into StarterPlayerScripts which then fires a remote event to the server and then make the brick go red when it receives this remote event