Which is better, while loop or InputEnded?

Please give your opinion on performance and optimization as well. Maybe even thread safety…

EDIT: event:FireServer() toggles whether not the action is ongoing on the server, that is why no arguments are passed.

Using a while loop (in this case a repeat until loop):

local debounce = true
userInputService.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	if not debounce and input.KeyCode == Enum.KeyCode.F then
		debounce = true
		event:FireServer()
		repeat task.wait(0.1) until not userInputService:IsKeyDown(Enum.KeyCode.F)
		event:FireServer()
	end
end)

Using InputEnded:

userInputService.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	if input.KeyCode == Enum.KeyCode.F then
		event:FireServer()
	end
end)

userInputService.InputEnded:Connect(function(input, gpe)
	if gpe then return end
	if input.KeyCode == Enum.KeyCode.F then
		event:FireServer()
	end
end)
2 Likes

in general, when coding you want your code to be made up mostly of signals to prevent code from running when it doesn’t have to. Using a loop here is obsolete especially since the InputEnded signal achieves the same thing

4 Likes