How in the world do I do keybinds?

I’m no scripter. I’m trying to make a FNAF style game, and I have most of the things figured out. But… I can’t figure out how to make it so when you click a keybind, the door closes or opens, or the camera opens. I want W for the front door, D for the right, A for the left, and S to open the camera. All the mouse-clicking works, but I cannot implement a keybind system on top of that for some reason. Any ideas?

3 Likes

Just use UserInpurService? It’ll work.

2 Likes
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
    if not gameProcessedEvent then
        if input.KeyCode = Enum.KeyCode.insertkeycodehere then
            -- code
        end
    end
end)
2 Likes

ill try that right now! thanks!

2 Likes

No problem! I edited my original script so that it wouldn’t run while a player is chatting.

2 Likes

Huh! that didn’t work. that genuinely was unexpected. I think it may be my code. Here it is.

db = false
door = false
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
if not gameProcessedEvent then
	if input.KeyCode = Enum.KeyCode.D then
	if not db then 
	db = true
	if not door then door = true
		script.Parent.Door.Sound:play()
		for i=0,15 do
			script.Parent.Door.CFrame = script.Parent.Door.CFrame - Vector3.new(0,0.8,0)
			wait()
		end
		script.Parent.Door.CFrame = script.Parent.Door.CFrame - Vector3.new(0,0.2,0)
		db = false
	elseif door then door = false
		script.Parent.Door.Sound:play()
		for i=0,15 do
			script.Parent.Door.CFrame = script.Parent.Door.CFrame + Vector3.new(0,0.8,0)
			wait()
		end
		script.Parent.Door.CFrame = script.Parent.Door.CFrame + Vector3.new(0,0.2,0)
	end
	db = false
end

end)

Try adding a print after if input.KeyCode == Enum.KeyCode.D to make sure that it is detecting your input

It isn’t detecting it. It says that the = sign in “if input.KeyCode = Enum.KeyCode.D then” is wrong, at least it is underlined in red.

You need to use two equals signs to check for equality

This also needs to be in a local script.

Where should I put it exactly?

You should use TweenService for moving the door like this.

1 Like

Oh the door script works fine, at least with clicking a button. The only problem is implementing a keybind on top of that.

Have you used any print statements to check if it is detecting your input? Just add print(“D key pressed”) and tell me if anything prints.

It works, but it isn’t necessarily performance efficient or a good coding habit. TweenService will smoothly move the part from one position to the other without having 15 separate tiny movements.

1 Like

Nah I tried that already. Nothing printed.

Also, the reason why this code doesn’t work is because you can’t have a server script listening for userinput or a local script moving parts! You need to create a remoteevent which tells the server the key press of the user from a local script.

1 Like
  1. Is this a local script?
  2. Are there any errors?

Yes it is in a local script. It’s a sibling of the door it is moving. Would that be ok?

1 Like

Could you please give your code again?

Edit: yes that is fine

1 Like