Wrong input performing action

Hello everyone!
I’ve got this script in which if the player presses the letter Q, their Character Model will teleport by 10 studs in the direction the Character’s head is pointing at, and it fortunately worked.
However, after the first teleport, every single input that the player gives will still teleport the player, even mouse inputs! I have a theory that this script is changing the way the character model behaves completely, but I am not 100% sure. How can I make only the letter Q teleport the player?

The script is a LocalScript set in the StarterCharacterScripts

local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local playerModel = game.Workspace:WaitForChild(Players.LocalPlayer.Name)
local rootPart = playerModel:WaitForChild("HumanoidRootPart")
local head = playerModel:WaitForChild("Head")


UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then 
		if input.KeyCode == Enum.KeyCode.Q then
			UIS.InputEnded:Connect(function()
				rootPart.CFrame = rootPart.CFrame + (head.CFrame.lookVector * 10)
			end)
		end
	end
end)
2 Likes

Its because of the InputEnded you have added, since it has no ifs it runs on any kind of input, also you are connecting it every time you press Q so you will just have more and more functions being connected. Just remove it and it should work, or if you want it to only teleport you when you stop holding Q then just replace the InputBegan with InputEnded.

If you want it to be holdable (it keeps teleporting while holding Q), try this out:

local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local playerModel = game.Workspace:WaitForChild(Players.LocalPlayer.Name)
local rootPart = playerModel:WaitForChild("HumanoidRootPart")
local head = playerModel:WaitForChild("Head")

UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then 
		if input.KeyCode == Enum.KeyCode.Q then
			repeat
				task.wait(.3)
				rootPart.CFrame = rootPart.CFrame + (head.CFrame.lookVector * 10)
			until not UIS:IsKeyDown(input.KeyCode)
		end
	end
end)

But shouldn’t the
if input.KeyCode == Enum.KeyCode.Q then
stop any other keycode from going through?

Yes but after that you are connecting an InputEnded function, which listens to every finished keypress since you dont have any if statements inside, and it is not getting disconnected at all, which means every time you press Q a new InputEnded function is being created, so just remove the whole InputEnded part and you should be good.

While that is true, keep in mind that the actual teleporting is inside

which doesn’t ever disconnect. You aren’t actually checking what key was released, and thus teleports the character whenever you release any kind of key.

Think of it like this. You listen for any key presses, check if it’s Q, then create a new RBXSignalConnection (InputEnded), which now listens for any kind of key release which in turn teleports the player.

So after the game checks once for the Q Keycode, it stops doing it because of the UIS.InputEnded:Connect(function() ?

I am using both InputBegan and InputEnded because in the future I will try doing a little animation for the character and slap it into the InputBegan. But I have fixed the issue with a Disconnect() function, thank you, both of you! @Downrest @realmile