Help with a key bind script

Hi Im trying to make it so that when a key is pressed it will make a frame green. But the issue is, when I hit the key it doesn’t work.

script

game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(key)
if key == "1" then
	    script.Parent.Parent.BackgroundColor3 = Color3.fromRGB(25, 255, 62)
	    script.Parent.Parent.Parent.Frame2.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
	    script.Parent.Parent.Parent.Frame3.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
	    script.Parent.Parent.Parent.Frame4.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
	    script.Parent.Parent.Parent.Frame5.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
   end
end)

locations

Screen Shot 2021-04-07 at 3.25.36 PM

I hope you can help me!

Maybe try using UserInputService instead.

1 Like

Use UserInputService or ContextActionService.

game:GetService("UserInputService").InputBegan:Connect(function(key, gp)
   if gp then return end -- stops the function if it's being processed by something else

   if key.KeyCode == Enum.KeyCode.One then
	    script.Parent.Parent.BackgroundColor3 = Color3.fromRGB(25, 255, 62)
	    script.Parent.Parent.Parent.Frame2.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
	    script.Parent.Parent.Parent.Frame3.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
	    script.Parent.Parent.Parent.Frame4.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
	    script.Parent.Parent.Parent.Frame5.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
   end
end)

The Mouse method is outdated.

No worries, my forum is being slow at the minute so my messages might appear late.

Hi,

can you give me an example? Im not really good at scripting. Ha ha ha.

oh thanks! Didn’t see that there.

where should I put the key I want to use?

oh wait. I see it. Its at the .one right? At the Enum beginning?

if key.KeyCode == Enum.KeyCode.One then

This line here ^. Replace the Enum.KeyCode.One to another Enum keycode.

example

if key.KeyCode == Enum.KeyCode.Z then -- detects when the "Z" key is pressed.

Here’s the enum keycode library.

oh. Okay. Thanks! It works too!

As already mentioned, it’s recommended you user UserInputService. Here’s an example of it’s use:

local UIS = game:GetService("UserInputService");

UIS.InputBegan:Connect(function(input,GP)
 if input.KeyCode == Enum.KeyCode.Q then --// There are a list of KeyCodes you can find, even "One".
        print("Pressed Q!"); 
    end;
end;

UserInputService contains many useful functions which is why I recommend switching over, and that Mouse is already deprecated so no need to use it anymore.