Input.KeyCode spamming

For some reason, when I’m testing, my inputs are spammed, so when I first press a button, it registers it as repeatedly being pressed. For instance, if I walk around, the print(input.KeyCode) line I have will spam print Enum.KeyCode.A, W, S and/or D.


While I do realize that the loop may be at fault for this, when I don’t have the loop, the code runs once and doesn’t repeatedly check if E is being pressed. How do I fix this?

uis.InputBegan:Connect(function(input)
	while true do
		print(input.KeyCode)
		wait(0.1)
		if GetDistance(workspace.SmallDoor.Keycard, hrp) <= 15 then
			
			
			part.Size = Vector3.new(GetDistance(workspace.SmallDoor.Keycard, hrp),0.1,0.1)
			local Center = (workspace.SmallDoor.Keycard.Position + hrp.Position)/2
			part.CFrame = CFrame.lookAt(Center, hrp.Position) --* CFrame.lookAt(workspace.SmallDoor.Keycard.Position, player.Character:FindFirstChild("HumanoidRootPart").Position)
			part.CFrame *= CFrame.Angles(0,math.rad(90),0)

			if (uis:GetFocusedTextBox()) then
				return;
			end
			local rep = false
			if input.KeyCode == Enum.KeyCode.E then
				if rep then return end
				rep = true
				print("E")
				local openDoorRequest = ReplicatedStorage:WaitForChild("eventDoorOpen")
				openDoorRequest:FireServer()--workspace.SmallDoor.Door)
				task.wait(2)
				rep = false
			end
		end
	end

end)

This code is meant to perform a simple task, when you press E while close enough to an object, do a thing, essentially. It does the thing fine, but it spams it.

Why not use a proximity prompt?

Mayby because you are looping through the same input in your while true do loop?

1 Like

This isn’t quite supposed to happen, taking into consideration that UserInputService.InputBegan runs every time some type of mouse/keyboard input is registered. That being said, you could maybe either use ProximityPrompts like DataSigh suggested or provide just a bit more context since I don’t really see anything that could prevent your code from being run more than once here.
And @O3_O2, trying to put whatever you want to do with the input in a while true do which needless to say is not ideal at all will do that in a new connection if I’m right, so what the OP probably meant is that they’re being spammed with several constant prints of their own input.

1 Like

Use a proximity prompt and customize its appearance.

You could use ProximityPrompts, like the people above mentioned. However, if you really want to use userinputservice, using a while loop is not recommended, if you want to do this, it is actually quite simple to do:

local uis = game:GetService("UserInputService")
local HRP = game:GetService("Players").LocalPlayer.Character:WaitForChild("HumanoidRootPart") -- getting the player's humanoidrootpart

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then -- if pressing e
		if (workspace.SmallDoor.Keycard.Position - HRP.Position).magnitude <= 15 then -- getting magnitude from the keycard position to the humanoidrootpart position, and if thta number is less than 15
			-- insert your stuff here
		end
	end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.