--Set up input began and ended for controller.

table.insert(EventsToDisconnect,UserInputService.InputBegan:Connect(function(Input,Processed)
	if Processed then return end
	local Character = Player.Character
	if not Character then return end
	local Humanoid = Character:FindFirstChildOfClass("Humanoid")
	if not Humanoid or Humanoid.Health <= 0 then return end
	
	--Disconnect the event if the tool is unparented.
	DisconnectEvents()
	
	--Register event if it was a mouse left click.
	if Input.UserInputType == Enum.UserInputType.MouseButton1 or Input.UserInputType == Enum.UserInputType.Touch then
		local Position = Get3DPosition(Input.Position.X,Input.Position.Y)
		LongInputBeganEvent:Fire(Position)
		
		if Input.UserInputType == Enum.UserInputType.MouseButton1 then
			InputBeganEvent:Fire(Position)
		end
	end
end))

table.insert(EventsToDisconnect,UserInputService.TouchTap:Connect(function(Positions,Processed)
	if Processed then return end
	local Character = Player.Character
	if not Character then return end
	local Humanoid = Character:FindFirstChildOfClass("Humanoid")
	if not Humanoid or Humanoid.Health <= 0 then return end
	
	--Disconnect the event if the tool is unparented.
	DisconnectEvents()
	
	local Position = Positions[1]
	if Position then
		InputBeganEvent:Fire(Get3DPosition(Position.X,Position.Y))
	end
end))

table.insert(EventsToDisconnect,UserInputService.InputEnded:Connect(function(Input)
	--Disconnect the event if the tool is unparented.
	DisconnectEvents()
	
	--Register event if it was a mouse left click or a touch.
	if Input.UserInputType == Enum.UserInputType.MouseButton1 or Input.UserInputType == Enum.UserInputType.Touch then
		InputEndedEvent:Fire(Get3DPosition(Input.Position.X,Input.Position.Y))
	end
end))

return InputHandler
