While loop not stopping

I am making a game, and I have a tool that while I am holding F, it should print something, but after I hold F, it keeps printing even though I’m not holding F anymore.

LocalScript

inputs.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if not player.Backpack:FindFirstChild("Wood sword") then
			while input.KeyCode == Enum.KeyCode.F do
				wait(.1)
				game.ReplicatedStorage.BlockEvent:FireServer()
			end
		end
	end
end)

ServerScript

game.ReplicatedStorage.BlockEvent.OnServerEvent:Connect(function(player)
	print("Block event has been recieved")
end)
1 Like

The problem is that you’re not updating the input.

Try

while UserInputService:IsKeyDown(Enum.KeyCode.F) do
    -- ...
end
2 Likes

InputObjects never change automatically, so you need to check if the user is still pressing that key a different way. Using @DiscoDino01’s solution would work fine, however there’s multiple solutions you could explore on your own.

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