Is this bad practice?

I’ve been nesting InputEndeds inside InputBegans for a while now. But it just dawned on me. Is there a chance that the InputEnded can happen before the connection gets initialized? I know it would be extremely unlikely but I’m using it for mouse buttons at the moment. And i’m worried autoclickers could mess everything up.

UserInputService.InputBegan:Connect(function(Input1, GameProcessed)
		--Start
		local EndConnection
		EndConnection = UserInputService.InputEnded:Connect(function(Input2, GameProcessed)
			if Input1 == Input2 then 
				--Stop
				EndConnection:Disconnect()
			end
		end)
	end)

Indeed it seems like a bad practice to me, as multiple InputBegans will cause a flood of InputEndeds that fire at once:
image

local UserInputService = game:GetService('UserInputService')

UserInputService.InputBegan:Connect(function(Input1, GameProcessed)
	--Start
	if Input1.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
	print(Input1.UserInputType, 'in')
	
	
	local EndConnection
	EndConnection = UserInputService.InputEnded:Connect(function(Input2, GameProcessed)
		if Input1 == Input2 then
			print(Input1.UserInputType, 'out')
			--Stop
			EndConnection:Disconnect()
		end
	end)
end)

whereas with separate input events this does not happen:
image

local UserInputService = game:GetService('UserInputService')

UserInputService.InputBegan:Connect(function(Input1, GameProcessed)
	if Input1.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
	
	print(Input1.UserInputType, 'in')
end)

UserInputService.InputEnded:Connect(function(Input2)
	if Input2.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
	
	print(Input2.UserInputType, 'out')
end)
1 Like

Yeah I agree. I’m also not a big fan of constantly making new connections every time input is made. Thanks for the opinion.

1 Like

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