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

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)



2 answers

Log in to vote
2
Answered by 1 year 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

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.

0
if you really want to change the part’s color to all players then use a remote event. T3_MasterGamer 2189 — 1y
0
Thank you! Cowgato 33 — 1y
Ad
Log in to vote
0
Answered by 1 year 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 — 1y
0
local scripts only run when in StarterGui, StarterPlayer, the players Character and the backpack R_LabradorRetriever 198 — 1y

Answer this question