Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

2 comments

0

if you really want to change the part’s color to all players then use a remote event.

0

Thank you!

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

2 comments

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?

0

local scripts only run when in StarterGui, StarterPlayer, the players Character and the backpack

This discussion is preserved as it was. New answers, comments, and votes are closed.