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:
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:
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)