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?)
01 | local debounce = false |
02 |
03 | local userinputservice = game:GetService( "UserInputService" ) |
04 |
05 | userinputservice.InputBegan:Connect( function (input, gameProcessedEvent) |
06 |
07 | if input.UserInputType = = Enum.UserInputType.Keyboard then |
08 |
09 | if input.KeyCode = = Enum.KeyCode.E then |
10 |
11 | if not debounce then |
12 |
13 | debounce = true |
14 |
15 | script.Parent.BrickColor = BrickColor.new( "Really red" ) |
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
01 | local debounce = false |
02 |
03 | local userinputservice = game:GetService( "UserInputService" ) |
04 |
05 | userinputservice.InputBegan:Connect( function (input, gameProcessedEvent) |
06 | if input.UserInputType = = Enum.UserInputType.Keyboard then |
07 | if input.KeyCode = = Enum.KeyCode.E then |
08 | if not debounce then |
09 | debounce = true |
10 | print ( 'My script works!' ) |
11 | debounce = false |
12 | end |
13 | end |
14 | end |
15 | 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