Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why does the brick color not change when a player presses 'E'?

Asked by
Cowgato 33
2 years ago

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

01local debounce = false
02 
03local userinputservice = game:GetService("UserInputService")
04 
05userinputservice.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")
View all 25 lines...

2 answers

Log in to vote
2
Answered by 2 years ago

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:

  • A Player’s Backpack, such as a child of a Tool
  • A Player’s character model
  • A Player’s PlayerGui
  • A Player’s PlayerScripts.
  • The ReplicatedFirst service

(provided from the docs)

So, when I run this localscript inside of StarterPlayerScripts

01local debounce = false
02 
03local userinputservice = game:GetService("UserInputService")
04 
05userinputservice.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
15end)

.. I get this result in the output:

My script works! - Client - LocalScript:10

Showing that the code above works completely fine.

0
if you really want to change the part’s color to all players then use a remote event. T3_MasterGamer 2189 — 2y
0
Thank you! Cowgato 33 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

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

0
Nope. Still didn't work after changing it to a local script. Do you think I should put in workspace instead? Or server script service? Cowgato 33 — 2y
0
local scripts only run when in StarterGui, StarterPlayer, the players Character and the backpack R_LabradorRetriever 198 — 2y

Answer this question